blob: ea6cb64dfb289afe590fa446e0d0c60d03ddedbd [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
Pali Rohár04aae282015-01-14 13:18:30 -0800246 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800249 if (ges && !fin)
250 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /*
253 * A "tap and drag" operation is reported by the hardware as a transition
254 * from (!fin && ges) to (fin && ges). This should be translated to the
255 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
256 */
257 if (ges && fin && !priv->prev_fin) {
258 input_report_abs(dev, ABS_X, x);
259 input_report_abs(dev, ABS_Y, y);
260 input_report_abs(dev, ABS_PRESSURE, 0);
261 input_report_key(dev, BTN_TOOL_FINGER, 0);
262 input_sync(dev);
263 }
264 priv->prev_fin = fin;
265
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800266 if (z > 30)
267 input_report_key(dev, BTN_TOUCH, 1);
268 if (z < 25)
269 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (z > 0) {
272 input_report_abs(dev, ABS_X, x);
273 input_report_abs(dev, ABS_Y, y);
274 }
275
276 input_report_abs(dev, ABS_PRESSURE, z);
277 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
278
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800279 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500280 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800282 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500283 input_report_key(dev, BTN_FORWARD, forward);
284 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800287 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800288 input_report_key(dev, BTN_0, packet[2] & 4);
289 input_report_key(dev, BTN_1, packet[0] & 0x10);
290 input_report_key(dev, BTN_2, packet[3] & 4);
291 input_report_key(dev, BTN_3, packet[0] & 0x20);
292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 input_sync(dev);
295}
296
Seth Forshee01ce6612011-11-07 19:54:13 -0800297/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800298 * Process bitmap data for V5 protocols. Return value is null.
299 *
300 * The bitmaps don't have enough data to track fingers, so this function
301 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700302 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800303 */
304static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700305 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800306{
307 int box_middle_x, box_middle_y;
308 unsigned int x_map, y_map;
309 unsigned char start_bit, end_bit;
310 unsigned char x_msb, x_lsb, y_msb, y_lsb;
311
312 x_map = fields->x_map;
313 y_map = fields->y_map;
314
315 if (!x_map || !y_map)
316 return;
317
318 /* Get Most-significant and Least-significant bit */
319 x_msb = fls(x_map);
320 x_lsb = ffs(x_map);
321 y_msb = fls(y_map);
322 y_lsb = ffs(y_map);
323
324 /* Most-significant bit should never exceed max sensor line number */
325 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
326 return;
327
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800328 if (fields->fingers > 1) {
329 start_bit = priv->x_bits - x_msb;
330 end_bit = priv->x_bits - x_lsb;
331 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
332 (2 * (priv->x_bits - 1));
333
334 start_bit = y_lsb - 1;
335 end_bit = y_msb - 1;
336 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
337 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700338 fields->mt[0] = fields->st;
339 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
340 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800341 }
342}
343
Hans de Goede036e6c72014-07-25 22:38:51 -0700344static void alps_get_bitmap_points(unsigned int map,
345 struct alps_bitmap_point *low,
346 struct alps_bitmap_point *high,
347 int *fingers)
348{
349 struct alps_bitmap_point *point;
350 int i, bit, prev_bit = 0;
351
352 point = low;
353 for (i = 0; map != 0; i++, map >>= 1) {
354 bit = map & 1;
355 if (bit) {
356 if (!prev_bit) {
357 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700358 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700359 (*fingers)++;
360 }
361 point->num_bits++;
362 } else {
363 if (prev_bit)
364 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700365 }
366 prev_bit = bit;
367 }
368}
369
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800370/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800371 * Process bitmap data from v3 and v4 protocols. Returns the number of
372 * fingers detected. A return value of 0 means at least one of the
373 * bitmaps was empty.
374 *
375 * The bitmaps don't have enough data to track fingers, so this function
376 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700377 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800378 * is greater than 0.
379 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800380static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700381 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800382{
Hans de Goede036e6c72014-07-25 22:38:51 -0700383 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800384 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
385 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800386
Hans de Goede02d04252014-07-25 22:43:35 -0700387 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800388 return 0;
389
Hans de Goede02d04252014-07-25 22:43:35 -0700390 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
391 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800392
393 /*
394 * Fingers can overlap, so we use the maximum count of fingers
395 * on either axis as the finger count.
396 */
397 fingers = max(fingers_x, fingers_y);
398
399 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700400 * If an axis reports only a single contact, we have overlapping or
401 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800402 */
Hans de Goede20bea682014-07-25 22:33:33 -0700403 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700404 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700405 x_low.num_bits = x_low.num_bits - i;
406 x_high.start_bit = x_low.start_bit + i;
407 x_high.num_bits = max(i, 1);
408 }
409 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700410 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700411 y_low.num_bits = y_low.num_bits - i;
412 y_high.start_bit = y_low.start_bit + i;
413 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800414 }
415
Hans de Goede02d04252014-07-25 22:43:35 -0700416 fields->mt[0].x =
417 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
418 (2 * (priv->x_bits - 1));
419 fields->mt[0].y =
420 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
421 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800422
Hans de Goede02d04252014-07-25 22:43:35 -0700423 fields->mt[1].x =
424 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
425 (2 * (priv->x_bits - 1));
426 fields->mt[1].y =
427 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
428 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800429
Hans de Goede40e8f532014-07-25 22:37:15 -0700430 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800431 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700432 fields->mt[0].y = priv->y_max - fields->mt[0].y;
433 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700434 }
435
Seth Forshee01ce6612011-11-07 19:54:13 -0800436 return fingers;
437}
438
Hans de Goedecdf333e2014-07-25 22:44:42 -0700439static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800440{
441 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700442 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
443 input_report_abs(dev, ABS_MT_POSITION_X, x);
444 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800445}
446
Hans de Goedecdf333e2014-07-25 22:44:42 -0700447static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800448{
Hans de Goede02d04252014-07-25 22:43:35 -0700449 struct alps_data *priv = psmouse->private;
450 struct input_dev *dev = psmouse->dev;
451 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700452 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700453
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800454 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700455 for (i = 0; i < n; i++)
456 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
457
458 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800459}
460
Hans de Goede68c21872014-07-25 22:47:25 -0700461static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
462{
463 struct alps_data *priv = psmouse->private;
464 struct input_dev *dev = psmouse->dev;
465 struct alps_fields *f = &priv->f;
466
467 /* Use st data when we don't have mt data */
468 if (fingers < 2) {
469 f->mt[0].x = f->st.x;
470 f->mt[0].y = f->st.y;
471 fingers = f->pressure > 0 ? 1 : 0;
472 }
473
Hans de Goede99d99962014-07-25 22:48:02 -0700474 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700475
476 input_mt_report_finger_count(dev, fingers);
477
478 input_report_key(dev, BTN_LEFT, f->left);
479 input_report_key(dev, BTN_RIGHT, f->right);
480 input_report_key(dev, BTN_MIDDLE, f->middle);
481
482 input_report_abs(dev, ABS_PRESSURE, f->pressure);
483
484 input_sync(dev);
485}
486
Seth Forshee25bded72011-11-07 19:53:36 -0800487static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
488{
489 struct alps_data *priv = psmouse->private;
490 unsigned char *packet = psmouse->packet;
491 struct input_dev *dev = priv->dev2;
492 int x, y, z, left, right, middle;
493
Pali Rohár34412ba2015-01-09 12:48:58 -0800494 /* It should be a DualPoint when received trackstick packet */
495 if (!(priv->flags & ALPS_DUALPOINT)) {
496 psmouse_warn(psmouse,
497 "Rejected trackstick packet from non DualPoint device");
498 return;
499 }
500
Seth Forshee25bded72011-11-07 19:53:36 -0800501 /* Sanity check packet */
502 if (!(packet[0] & 0x40)) {
503 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
504 return;
505 }
506
507 /*
508 * There's a special packet that seems to indicate the end
509 * of a stream of trackstick data. Filter these out.
510 */
511 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
512 return;
513
514 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
515 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
516 z = (packet[4] & 0x7c) >> 2;
517
518 /*
519 * The x and y values tend to be quite large, and when used
520 * alone the trackstick is difficult to use. Scale them down
521 * to compensate.
522 */
523 x /= 8;
524 y /= 8;
525
526 input_report_rel(dev, REL_X, x);
527 input_report_rel(dev, REL_Y, -y);
528
529 /*
530 * Most ALPS models report the trackstick buttons in the touchpad
531 * packets, but a few report them here. No reliable way has been
532 * found to differentiate between the models upfront, so we enable
533 * the quirk in response to seeing a button press in the trackstick
534 * packet.
535 */
536 left = packet[3] & 0x01;
537 right = packet[3] & 0x02;
538 middle = packet[3] & 0x04;
539
540 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
541 (left || right || middle))
542 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
543
544 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
545 input_report_key(dev, BTN_LEFT, left);
546 input_report_key(dev, BTN_RIGHT, right);
547 input_report_key(dev, BTN_MIDDLE, middle);
548 }
549
550 input_sync(dev);
551 return;
552}
553
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800554static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
555{
556 f->left = !!(p[3] & 0x01);
557 f->right = !!(p[3] & 0x02);
558 f->middle = !!(p[3] & 0x04);
559
560 f->ts_left = !!(p[3] & 0x10);
561 f->ts_right = !!(p[3] & 0x20);
562 f->ts_middle = !!(p[3] & 0x40);
563}
564
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700565static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800566 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800567{
568 f->first_mp = !!(p[4] & 0x40);
569 f->is_mp = !!(p[0] & 0x40);
570
571 f->fingers = (p[5] & 0x3) + 1;
572 f->x_map = ((p[4] & 0x7e) << 8) |
573 ((p[1] & 0x7f) << 2) |
574 ((p[0] & 0x30) >> 4);
575 f->y_map = ((p[3] & 0x70) << 4) |
576 ((p[2] & 0x7f) << 1) |
577 (p[4] & 0x01);
578
Hans de Goede02d04252014-07-25 22:43:35 -0700579 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800580 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700581 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
582 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800583
584 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700585
586 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800587}
588
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700589static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800590 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800591{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800592 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800593
Yunkang Tangf105e342014-07-25 22:29:24 -0700594 /* Rushmore's packet decode has a bit difference with Pinnacle's */
595 f->is_mp = !!(p[5] & 0x40);
596 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800597 f->x_map |= (p[5] & 0x10) << 11;
598 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700599
600 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800601}
602
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700603static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800604 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800605{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800606 u64 palm_data = 0;
607 struct alps_data *priv = psmouse->private;
608
Dave Turvene75af9e52013-02-21 22:58:28 -0800609 f->first_mp = !!(p[0] & 0x02);
610 f->is_mp = !!(p[0] & 0x20);
611
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800612 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700613 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
614 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
615 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800616 alps_decode_buttons_v3(f, p);
617 } else {
618 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800619 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800620
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800621 palm_data = (p[1] & 0x7f) |
622 ((p[2] & 0x7f) << 7) |
623 ((p[4] & 0x7f) << 14) |
624 ((p[5] & 0x7f) << 21) |
625 ((p[3] & 0x07) << 28) |
626 (((u64)p[3] & 0x70) << 27) |
627 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800628
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800629 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
630 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
631
632 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
633 f->x_map = (palm_data >> priv->y_bits) &
634 (BIT(priv->x_bits) - 1);
635 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700636
637 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800638}
639
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800640static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800641{
642 struct alps_data *priv = psmouse->private;
643 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800644 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700645 struct alps_fields *f = &priv->f;
646 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800647
Hans de Goede02d04252014-07-25 22:43:35 -0700648 memset(f, 0, sizeof(*f));
649
650 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800651
652 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800653 * There's no single feature of touchpad position and bitmap packets
654 * that can be used to distinguish between them. We rely on the fact
655 * that a bitmap packet should always follow a position packet with
656 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800657 */
658 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800659 /*
660 * Sometimes a position packet will indicate a multi-packet
661 * sequence, but then what follows is another position
662 * packet. Check for this, and when it happens process the
663 * position packet as usual.
664 */
Hans de Goede02d04252014-07-25 22:43:35 -0700665 if (f->is_mp) {
666 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800667 if (priv->proto_version == ALPS_PROTO_V3 ||
668 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700669 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700670 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800671
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800672 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700673 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800674 psmouse);
675 } else {
676 /*
677 * Because Dolphin uses position packet's
678 * coordinate data as Pt1 and uses it to
679 * calculate Pt2, so we need to do position
680 * packet decode first.
681 */
Hans de Goede02d04252014-07-25 22:43:35 -0700682 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800683 psmouse);
684
685 /*
686 * Since Dolphin's finger number is reliable,
687 * there is no need to compare with bmap_fn.
688 */
Hans de Goede02d04252014-07-25 22:43:35 -0700689 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800690 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800691 } else {
692 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800693 }
694 }
695
Seth Forshee01ce6612011-11-07 19:54:13 -0800696 /*
697 * Bit 6 of byte 0 is not usually set in position packets. The only
698 * times it seems to be set is in situations where the data is
699 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
700 * this combined with the fact that this bit is useful for filtering
701 * out misidentified bitmap packets, we reject anything with this
702 * bit set.
703 */
Hans de Goede02d04252014-07-25 22:43:35 -0700704 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800705 return;
706
Hans de Goede02d04252014-07-25 22:43:35 -0700707 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800708 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800709 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
710 return;
711 }
712
713 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800714
Seth Forshee25bded72011-11-07 19:53:36 -0800715 /*
716 * Sometimes the hardware sends a single packet with z = 0
717 * in the middle of a stream. Real releases generate packets
718 * with x, y, and z all zero, so these seem to be flukes.
719 * Ignore them.
720 */
Hans de Goede02d04252014-07-25 22:43:35 -0700721 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800722 return;
723
Hans de Goede68c21872014-07-25 22:47:25 -0700724 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800725
Pali Rohár34412ba2015-01-09 12:48:58 -0800726 if ((priv->flags & ALPS_DUALPOINT) &&
727 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700728 input_report_key(dev2, BTN_LEFT, f->ts_left);
729 input_report_key(dev2, BTN_RIGHT, f->ts_right);
730 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800731 input_sync(dev2);
732 }
733}
734
735static void alps_process_packet_v3(struct psmouse *psmouse)
736{
737 unsigned char *packet = psmouse->packet;
738
739 /*
740 * v3 protocol packets come in three types, two representing
741 * touchpad data and one representing trackstick data.
742 * Trackstick packets seem to be distinguished by always
743 * having 0x3f in the last byte. This value has never been
744 * observed in the last byte of either of the other types
745 * of packets.
746 */
747 if (packet[5] == 0x3f) {
748 alps_process_trackstick_packet_v3(psmouse);
749 return;
750 }
751
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800752 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800753}
754
Yunkang Tang95f75e92013-12-01 22:33:52 -0800755static void alps_process_packet_v6(struct psmouse *psmouse)
756{
757 struct alps_data *priv = psmouse->private;
758 unsigned char *packet = psmouse->packet;
759 struct input_dev *dev = psmouse->dev;
760 struct input_dev *dev2 = priv->dev2;
761 int x, y, z, left, right, middle;
762
763 /*
764 * We can use Byte5 to distinguish if the packet is from Touchpad
765 * or Trackpoint.
766 * Touchpad: 0 - 0x7E
767 * Trackpoint: 0x7F
768 */
769 if (packet[5] == 0x7F) {
770 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800771 if (!(priv->flags & ALPS_DUALPOINT)) {
772 psmouse_warn(psmouse,
773 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800774 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800775 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800776
777 /* Trackpoint packet */
778 x = packet[1] | ((packet[3] & 0x20) << 2);
779 y = packet[2] | ((packet[3] & 0x40) << 1);
780 z = packet[4];
781 left = packet[3] & 0x01;
782 right = packet[3] & 0x02;
783 middle = packet[3] & 0x04;
784
785 /* To prevent the cursor jump when finger lifted */
786 if (x == 0x7F && y == 0x7F && z == 0x7F)
787 x = y = z = 0;
788
789 /* Divide 4 since trackpoint's speed is too fast */
790 input_report_rel(dev2, REL_X, (char)x / 4);
791 input_report_rel(dev2, REL_Y, -((char)y / 4));
792
793 input_report_key(dev2, BTN_LEFT, left);
794 input_report_key(dev2, BTN_RIGHT, right);
795 input_report_key(dev2, BTN_MIDDLE, middle);
796
797 input_sync(dev2);
798 return;
799 }
800
801 /* Touchpad packet */
802 x = packet[1] | ((packet[3] & 0x78) << 4);
803 y = packet[2] | ((packet[4] & 0x78) << 4);
804 z = packet[5];
805 left = packet[3] & 0x01;
806 right = packet[3] & 0x02;
807
808 if (z > 30)
809 input_report_key(dev, BTN_TOUCH, 1);
810 if (z < 25)
811 input_report_key(dev, BTN_TOUCH, 0);
812
813 if (z > 0) {
814 input_report_abs(dev, ABS_X, x);
815 input_report_abs(dev, ABS_Y, y);
816 }
817
818 input_report_abs(dev, ABS_PRESSURE, z);
819 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
820
821 /* v6 touchpad does not have middle button */
822 input_report_key(dev, BTN_LEFT, left);
823 input_report_key(dev, BTN_RIGHT, right);
824
825 input_sync(dev);
826}
827
Seth Forshee25bded72011-11-07 19:53:36 -0800828static void alps_process_packet_v4(struct psmouse *psmouse)
829{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700830 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800831 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700832 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700833 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700834
835 /*
836 * v4 has a 6-byte encoding for bitmap data, but this data is
837 * broken up between 3 normal packets. Use priv->multi_packet to
838 * track our position in the bitmap packet.
839 */
840 if (packet[6] & 0x40) {
841 /* sync, reset position */
842 priv->multi_packet = 0;
843 }
844
845 if (WARN_ON_ONCE(priv->multi_packet > 2))
846 return;
847
848 offset = 2 * priv->multi_packet;
849 priv->multi_data[offset] = packet[6];
850 priv->multi_data[offset + 1] = packet[7];
851
852 if (++priv->multi_packet > 2) {
853 priv->multi_packet = 0;
854
Hans de Goede02d04252014-07-25 22:43:35 -0700855 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700856 ((priv->multi_data[3] & 0x60) << 3) |
857 ((priv->multi_data[0] & 0x3f) << 2) |
858 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700859 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700860 ((priv->multi_data[3] & 0x1f) << 5) |
861 (priv->multi_data[1] & 0x1f);
862
Hans de Goede02d04252014-07-25 22:43:35 -0700863 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700864 }
Seth Forshee25bded72011-11-07 19:53:36 -0800865
Andreas Boschb0cfb792014-10-15 10:44:50 -0700866 f->left = !!(packet[4] & 0x01);
867 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800868
Hans de Goede02d04252014-07-25 22:43:35 -0700869 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
870 ((packet[0] & 0x30) >> 4);
871 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
872 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800873
Hans de Goede68c21872014-07-25 22:47:25 -0700874 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800875}
876
Yunkang Tang38088432014-07-26 13:51:41 -0700877static bool alps_is_valid_package_v7(struct psmouse *psmouse)
878{
879 switch (psmouse->pktcnt) {
880 case 3:
881 return (psmouse->packet[2] & 0x40) == 0x40;
882 case 4:
883 return (psmouse->packet[3] & 0x48) == 0x48;
884 case 6:
885 return (psmouse->packet[5] & 0x40) == 0x00;
886 }
887 return true;
888}
889
890static unsigned char alps_get_packet_id_v7(char *byte)
891{
892 unsigned char packet_id;
893
894 if (byte[4] & 0x40)
895 packet_id = V7_PACKET_ID_TWO;
896 else if (byte[4] & 0x01)
897 packet_id = V7_PACKET_ID_MULTI;
898 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
899 packet_id = V7_PACKET_ID_NEW;
900 else if (byte[1] == 0x00 && byte[4] == 0x00)
901 packet_id = V7_PACKET_ID_IDLE;
902 else
903 packet_id = V7_PACKET_ID_UNKNOWN;
904
905 return packet_id;
906}
907
908static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
909 unsigned char *pkt,
910 unsigned char pkt_id)
911{
912 mt[0].x = ((pkt[2] & 0x80) << 4);
913 mt[0].x |= ((pkt[2] & 0x3F) << 5);
914 mt[0].x |= ((pkt[3] & 0x30) >> 1);
915 mt[0].x |= (pkt[3] & 0x07);
916 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
917
918 mt[1].x = ((pkt[3] & 0x80) << 4);
919 mt[1].x |= ((pkt[4] & 0x80) << 3);
920 mt[1].x |= ((pkt[4] & 0x3F) << 4);
921 mt[1].y = ((pkt[5] & 0x80) << 3);
922 mt[1].y |= ((pkt[5] & 0x3F) << 4);
923
924 switch (pkt_id) {
925 case V7_PACKET_ID_TWO:
926 mt[1].x &= ~0x000F;
927 mt[1].y |= 0x000F;
928 break;
929
930 case V7_PACKET_ID_MULTI:
931 mt[1].x &= ~0x003F;
932 mt[1].y &= ~0x0020;
933 mt[1].y |= ((pkt[4] & 0x02) << 4);
934 mt[1].y |= 0x001F;
935 break;
936
937 case V7_PACKET_ID_NEW:
938 mt[1].x &= ~0x003F;
939 mt[1].x |= (pkt[0] & 0x20);
940 mt[1].y |= 0x000F;
941 break;
942 }
943
944 mt[0].y = 0x7FF - mt[0].y;
945 mt[1].y = 0x7FF - mt[1].y;
946}
947
948static int alps_get_mt_count(struct input_mt_pos *mt)
949{
Hans de Goede7091c442014-12-18 09:53:34 -0800950 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700951
Hans de Goede7091c442014-12-18 09:53:34 -0800952 for (i = 0; i < MAX_TOUCHES; i++) {
953 if (mt[i].x != 0 || mt[i].y != 0)
954 fingers++;
955 }
Yunkang Tang38088432014-07-26 13:51:41 -0700956
Hans de Goede7091c442014-12-18 09:53:34 -0800957 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700958}
959
960static int alps_decode_packet_v7(struct alps_fields *f,
961 unsigned char *p,
962 struct psmouse *psmouse)
963{
Hans de Goeded27eb792014-12-18 09:55:14 -0800964 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700965 unsigned char pkt_id;
966
967 pkt_id = alps_get_packet_id_v7(p);
968 if (pkt_id == V7_PACKET_ID_IDLE)
969 return 0;
970 if (pkt_id == V7_PACKET_ID_UNKNOWN)
971 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800972 /*
973 * NEW packets are send to indicate a discontinuity in the finger
974 * coordinate reporting. Specifically a finger may have moved from
975 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
976 * us.
977 *
978 * NEW packets have 3 problems:
979 * 1) They do not contain middle / right button info (on non clickpads)
980 * this can be worked around by preserving the old button state
981 * 2) They do not contain an accurate fingercount, and they are
982 * typically send when the number of fingers changes. We cannot use
983 * the old finger count as that may mismatch with the amount of
984 * touch coordinates we've available in the NEW packet
985 * 3) Their x data for the second touch is inaccurate leading to
986 * a possible jump of the x coordinate by 16 units when the first
987 * non NEW packet comes in
988 * Since problems 2 & 3 cannot be worked around, just ignore them.
989 */
990 if (pkt_id == V7_PACKET_ID_NEW)
991 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -0700992
993 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
994
Yunkang Tang38088432014-07-26 13:51:41 -0700995 if (pkt_id == V7_PACKET_ID_TWO)
996 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -0800997 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -0700998 f->fingers = 3 + (p[5] & 0x03);
999
Hans de Goeded27eb792014-12-18 09:55:14 -08001000 f->left = (p[0] & 0x80) >> 7;
1001 if (priv->flags & ALPS_BUTTONPAD) {
1002 if (p[0] & 0x20)
1003 f->fingers++;
1004 if (p[0] & 0x10)
1005 f->fingers++;
1006 } else {
1007 f->right = (p[0] & 0x20) >> 5;
1008 f->middle = (p[0] & 0x10) >> 4;
1009 }
1010
Hans de Goede7091c442014-12-18 09:53:34 -08001011 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1012 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1013 f->mt[0].x = f->mt[1].x;
1014 f->mt[0].y = f->mt[1].y;
1015 f->mt[1].x = 0;
1016 f->mt[1].y = 0;
1017 }
1018
Yunkang Tang38088432014-07-26 13:51:41 -07001019 return 0;
1020}
1021
1022static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1023{
1024 struct alps_data *priv = psmouse->private;
1025 unsigned char *packet = psmouse->packet;
1026 struct input_dev *dev2 = priv->dev2;
1027 int x, y, z, left, right, middle;
1028
Pali Rohár34412ba2015-01-09 12:48:58 -08001029 /* It should be a DualPoint when received trackstick packet */
1030 if (!(priv->flags & ALPS_DUALPOINT)) {
1031 psmouse_warn(psmouse,
1032 "Rejected trackstick packet from non DualPoint device");
1033 return;
1034 }
1035
Yunkang Tang38088432014-07-26 13:51:41 -07001036 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1037 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1038 ((packet[3] & 0x20) << 1);
1039 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1040
1041 left = (packet[1] & 0x01);
1042 right = (packet[1] & 0x02) >> 1;
1043 middle = (packet[1] & 0x04) >> 2;
1044
1045 /* Divide 2 since trackpoint's speed is too fast */
1046 input_report_rel(dev2, REL_X, (char)x / 2);
1047 input_report_rel(dev2, REL_Y, -((char)y / 2));
1048
1049 input_report_key(dev2, BTN_LEFT, left);
1050 input_report_key(dev2, BTN_RIGHT, right);
1051 input_report_key(dev2, BTN_MIDDLE, middle);
1052
1053 input_sync(dev2);
1054}
1055
1056static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1057{
1058 struct alps_data *priv = psmouse->private;
1059 struct input_dev *dev = psmouse->dev;
1060 struct alps_fields *f = &priv->f;
1061
1062 memset(f, 0, sizeof(*f));
1063
1064 if (priv->decode_fields(f, psmouse->packet, psmouse))
1065 return;
1066
1067 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1068
1069 input_mt_report_finger_count(dev, f->fingers);
1070
1071 input_report_key(dev, BTN_LEFT, f->left);
1072 input_report_key(dev, BTN_RIGHT, f->right);
1073 input_report_key(dev, BTN_MIDDLE, f->middle);
1074
1075 input_sync(dev);
1076}
1077
1078static void alps_process_packet_v7(struct psmouse *psmouse)
1079{
1080 unsigned char *packet = psmouse->packet;
1081
1082 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1083 alps_process_trackstick_packet_v7(psmouse);
1084 else
1085 alps_process_touchpad_packet_v7(psmouse);
1086}
1087
Pali Rohár04aae282015-01-14 13:18:30 -08001088static DEFINE_MUTEX(alps_mutex);
1089
1090static void alps_register_bare_ps2_mouse(struct work_struct *work)
1091{
1092 struct alps_data *priv =
1093 container_of(work, struct alps_data, dev3_register_work.work);
1094 struct psmouse *psmouse = priv->psmouse;
1095 struct input_dev *dev3;
1096 int error = 0;
1097
1098 mutex_lock(&alps_mutex);
1099
1100 if (priv->dev3)
1101 goto out;
1102
1103 dev3 = input_allocate_device();
1104 if (!dev3) {
1105 psmouse_err(psmouse, "failed to allocate secondary device\n");
1106 error = -ENOMEM;
1107 goto out;
1108 }
1109
1110 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1111 psmouse->ps2dev.serio->phys,
1112 (priv->dev2 ? "input2" : "input1"));
1113 dev3->phys = priv->phys3;
1114
1115 /*
1116 * format of input device name is: "protocol vendor name"
1117 * see function psmouse_switch_protocol() in psmouse-base.c
1118 */
1119 dev3->name = "PS/2 ALPS Mouse";
1120
1121 dev3->id.bustype = BUS_I8042;
1122 dev3->id.vendor = 0x0002;
1123 dev3->id.product = PSMOUSE_PS2;
1124 dev3->id.version = 0x0000;
1125 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1126
1127 input_set_capability(dev3, EV_REL, REL_X);
1128 input_set_capability(dev3, EV_REL, REL_Y);
1129 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1130 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1131 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1132
1133 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1134
1135 error = input_register_device(dev3);
1136 if (error) {
1137 psmouse_err(psmouse,
1138 "failed to register secondary device: %d\n",
1139 error);
1140 input_free_device(dev3);
1141 goto out;
1142 }
1143
1144 priv->dev3 = dev3;
1145
1146out:
1147 /*
1148 * Save the error code so that we can detect that we
1149 * already tried to create the device.
1150 */
1151 if (error)
1152 priv->dev3 = ERR_PTR(error);
1153
1154 mutex_unlock(&alps_mutex);
1155}
1156
Hans de Goede59c30af2015-04-03 17:14:40 -07001157static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001158 unsigned char packet[],
1159 bool report_buttons)
1160{
Hans de Goede59c30af2015-04-03 17:14:40 -07001161 struct alps_data *priv = psmouse->private;
Hans de Goede6bcca192015-04-08 09:26:42 -07001162 struct input_dev *dev, *dev2 = NULL;
Hans de Goede59c30af2015-04-03 17:14:40 -07001163
Hans de Goedee3a79212015-04-03 17:20:05 -07001164 /* Figure out which device to use to report the bare packet */
1165 if (priv->proto_version == ALPS_PROTO_V2 &&
1166 (priv->flags & ALPS_DUALPOINT)) {
1167 /* On V2 devices the DualPoint Stick reports bare packets */
1168 dev = priv->dev2;
Hans de Goede6bcca192015-04-08 09:26:42 -07001169 dev2 = psmouse->dev;
Hans de Goedee3a79212015-04-03 17:20:05 -07001170 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001171 /* Register dev3 mouse if we received PS/2 packet first time */
1172 if (!IS_ERR(priv->dev3))
1173 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1174 0);
1175 return;
1176 } else {
1177 dev = priv->dev3;
1178 }
1179
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001180 if (report_buttons)
Hans de Goede6bcca192015-04-08 09:26:42 -07001181 alps_report_buttons(dev, dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001182 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1183
Pali Rohár04aae282015-01-14 13:18:30 -08001184 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001185 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001186 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001187 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1188
Pali Rohár04aae282015-01-14 13:18:30 -08001189 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001190}
1191
1192static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct alps_data *priv = psmouse->private;
1195
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001196 if (psmouse->pktcnt < 6)
1197 return PSMOUSE_GOOD_DATA;
1198
1199 if (psmouse->pktcnt == 6) {
1200 /*
1201 * Start a timer to flush the packet if it ends up last
1202 * 6-byte packet in the stream. Timer needs to fire
1203 * psmouse core times out itself. 20 ms should be enough
1204 * to decide if we are getting more data or not.
1205 */
1206 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1207 return PSMOUSE_GOOD_DATA;
1208 }
1209
1210 del_timer(&priv->timer);
1211
1212 if (psmouse->packet[6] & 0x80) {
1213
1214 /*
1215 * Highest bit is set - that means we either had
1216 * complete ALPS packet and this is start of the
1217 * next packet or we got garbage.
1218 */
1219
1220 if (((psmouse->packet[3] |
1221 psmouse->packet[4] |
1222 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001223 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001224 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001225 "refusing packet %4ph (suspected interleaved ps/2)\n",
1226 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001227 return PSMOUSE_BAD_DATA;
1228 }
1229
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001230 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001231
1232 /* Continue with the next packet */
1233 psmouse->packet[0] = psmouse->packet[6];
1234 psmouse->pktcnt = 1;
1235
1236 } else {
1237
1238 /*
1239 * High bit is 0 - that means that we indeed got a PS/2
1240 * packet in the middle of ALPS packet.
1241 *
1242 * There is also possibility that we got 6-byte ALPS
1243 * packet followed by 3-byte packet from trackpoint. We
1244 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001245 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001246 * normal operation (user would need to press all
1247 * buttons on the pad and start moving trackpoint
1248 * without touching the pad surface) we assume former.
1249 * Even if we are wrong the wost thing that would happen
1250 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001251 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001252 */
1253
Hans de Goede59c30af2015-04-03 17:14:40 -07001254 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1255 false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001256
1257 /*
1258 * Continue with the standard ALPS protocol handling,
1259 * but make sure we won't process it as an interleaved
1260 * packet again, which may happen if all buttons are
1261 * pressed. To avoid this let's reset the 4th bit which
1262 * is normally 1.
1263 */
1264 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1265 psmouse->pktcnt = 4;
1266 }
1267
1268 return PSMOUSE_GOOD_DATA;
1269}
1270
1271static void alps_flush_packet(unsigned long data)
1272{
1273 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001274 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001275
1276 serio_pause_rx(psmouse->ps2dev.serio);
1277
Seth Forsheeb46615f2011-11-07 19:53:30 -08001278 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001279
1280 /*
1281 * We did not any more data in reasonable amount of time.
1282 * Validate the last 3 bytes and process as a standard
1283 * ALPS packet.
1284 */
1285 if ((psmouse->packet[3] |
1286 psmouse->packet[4] |
1287 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001288 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001289 "refusing packet %3ph (suspected interleaved ps/2)\n",
1290 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001291 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001292 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001293 }
1294 psmouse->pktcnt = 0;
1295 }
1296
1297 serio_continue_rx(psmouse->ps2dev.serio);
1298}
1299
1300static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1301{
1302 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001303
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001304 /*
1305 * Check if we are dealing with a bare PS/2 packet, presumably from
1306 * a device connected to the external PS/2 port. Because bare PS/2
1307 * protocol does not have enough constant bits to self-synchronize
1308 * properly we only do this if the device is fully synchronized.
1309 */
1310 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (psmouse->pktcnt == 3) {
Hans de Goede59c30af2015-04-03 17:14:40 -07001312 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1313 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return PSMOUSE_FULL_PACKET;
1315 }
1316 return PSMOUSE_GOOD_DATA;
1317 }
1318
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001319 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1320
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001321 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001322 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1323 return alps_handle_interleaved_ps2(psmouse);
1324 }
1325
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001326 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001327 psmouse_dbg(psmouse,
1328 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001329 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Seth Forsheeb46615f2011-11-07 19:53:30 -08001333 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001334 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001335 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001336 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001337 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1338 psmouse->pktcnt - 1,
1339 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001340
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001341 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001342 psmouse->pktcnt == psmouse->pktsize) {
1343 /*
1344 * Some Dell boxes, such as Latitude E6440 or E7440
1345 * with closed lid, quite often smash last byte of
1346 * otherwise valid packet with 0xff. Given that the
1347 * next packet is very likely to be valid let's
1348 * report PSMOUSE_FULL_PACKET but not process data,
1349 * rather than reporting PSMOUSE_BAD_DATA and
1350 * filling the logs.
1351 */
1352 return PSMOUSE_FULL_PACKET;
1353 }
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Yunkang Tang38088432014-07-26 13:51:41 -07001358 if (priv->proto_version == ALPS_PROTO_V7 &&
1359 !alps_is_valid_package_v7(psmouse)) {
1360 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1361 psmouse->pktcnt - 1,
1362 psmouse->packet[psmouse->pktcnt - 1]);
1363 return PSMOUSE_BAD_DATA;
1364 }
1365
Seth Forsheeb46615f2011-11-07 19:53:30 -08001366 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001367 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return PSMOUSE_FULL_PACKET;
1369 }
1370
1371 return PSMOUSE_GOOD_DATA;
1372}
1373
Seth Forshee25bded72011-11-07 19:53:36 -08001374static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1375{
1376 struct ps2dev *ps2dev = &psmouse->ps2dev;
1377 struct alps_data *priv = psmouse->private;
1378 int command;
1379 unsigned char *param;
1380 unsigned char dummy[4];
1381
1382 BUG_ON(nibble > 0xf);
1383
1384 command = priv->nibble_commands[nibble].command;
1385 param = (command & 0x0f00) ?
1386 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1387
1388 if (ps2_command(ps2dev, param, command))
1389 return -1;
1390
1391 return 0;
1392}
1393
1394static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1395{
1396 struct ps2dev *ps2dev = &psmouse->ps2dev;
1397 struct alps_data *priv = psmouse->private;
1398 int i, nibble;
1399
1400 if (ps2_command(ps2dev, NULL, priv->addr_command))
1401 return -1;
1402
1403 for (i = 12; i >= 0; i -= 4) {
1404 nibble = (addr >> i) & 0xf;
1405 if (alps_command_mode_send_nibble(psmouse, nibble))
1406 return -1;
1407 }
1408
1409 return 0;
1410}
1411
1412static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1413{
1414 struct ps2dev *ps2dev = &psmouse->ps2dev;
1415 unsigned char param[4];
1416
1417 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1418 return -1;
1419
1420 /*
1421 * The address being read is returned in the first two bytes
1422 * of the result. Check that this address matches the expected
1423 * address.
1424 */
1425 if (addr != ((param[0] << 8) | param[1]))
1426 return -1;
1427
1428 return param[2];
1429}
1430
1431static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1432{
1433 if (alps_command_mode_set_addr(psmouse, addr))
1434 return -1;
1435 return __alps_command_mode_read_reg(psmouse, addr);
1436}
1437
1438static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1439{
1440 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1441 return -1;
1442 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1443 return -1;
1444 return 0;
1445}
1446
1447static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1448 u8 value)
1449{
1450 if (alps_command_mode_set_addr(psmouse, addr))
1451 return -1;
1452 return __alps_command_mode_write_reg(psmouse, value);
1453}
1454
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001455static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1456 int repeated_command, unsigned char *param)
1457{
1458 struct ps2dev *ps2dev = &psmouse->ps2dev;
1459
1460 param[0] = 0;
1461 if (init_command && ps2_command(ps2dev, param, init_command))
1462 return -EIO;
1463
1464 if (ps2_command(ps2dev, NULL, repeated_command) ||
1465 ps2_command(ps2dev, NULL, repeated_command) ||
1466 ps2_command(ps2dev, NULL, repeated_command))
1467 return -EIO;
1468
1469 param[0] = param[1] = param[2] = 0xff;
1470 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1471 return -EIO;
1472
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001473 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1474 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001475 return 0;
1476}
1477
Yunkang Tang38088432014-07-26 13:51:41 -07001478static bool alps_check_valid_firmware_id(unsigned char id[])
1479{
1480 if (id[0] == 0x73)
1481 return true;
1482
1483 if (id[0] == 0x88 &&
1484 (id[1] == 0x07 ||
1485 id[1] == 0x08 ||
1486 (id[1] & 0xf0) == 0xb0 ||
1487 (id[1] & 0xf0) == 0xc0)) {
1488 return true;
1489 }
1490
1491 return false;
1492}
1493
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001494static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001495{
1496 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001497
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001498 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001499 psmouse_err(psmouse, "failed to enter command mode\n");
1500 return -1;
1501 }
1502
Yunkang Tang38088432014-07-26 13:51:41 -07001503 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001504 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001505 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001506 return -1;
1507 }
Seth Forshee25bded72011-11-07 19:53:36 -08001508 return 0;
1509}
1510
1511static inline int alps_exit_command_mode(struct psmouse *psmouse)
1512{
1513 struct ps2dev *ps2dev = &psmouse->ps2dev;
1514 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1515 return -1;
1516 return 0;
1517}
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/*
1520 * For DualPoint devices select the device that should respond to
1521 * subsequent commands. It looks like glidepad is behind stickpointer,
1522 * I'd thought it would be other way around...
1523 */
Seth Forshee25bded72011-11-07 19:53:36 -08001524static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1528
1529 if (ps2_command(ps2dev, NULL, cmd) ||
1530 ps2_command(ps2dev, NULL, cmd) ||
1531 ps2_command(ps2dev, NULL, cmd) ||
1532 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1533 return -1;
1534
1535 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001536 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 return 0;
1539}
1540
Seth Forshee25bded72011-11-07 19:53:36 -08001541static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct ps2dev *ps2dev = &psmouse->ps2dev;
1544
1545 /* Try ALPS magic knock - 4 disable before enable */
1546 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1547 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1548 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1549 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1550 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1551 return -1;
1552
1553 /*
1554 * Switch mouse to poll (remote) mode so motion data will not
1555 * get in our way
1556 */
1557 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1558}
1559
Yunkang Tang95f75e92013-12-01 22:33:52 -08001560static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1561{
1562 int i, nibble;
1563
1564 /*
1565 * b0-b11 are valid bits, send sequence is inverse.
1566 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1567 */
1568 for (i = 0; i <= 8; i += 4) {
1569 nibble = (word >> i) & 0xf;
1570 if (alps_command_mode_send_nibble(psmouse, nibble))
1571 return -1;
1572 }
1573
1574 return 0;
1575}
1576
1577static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1578 u16 addr, u16 value)
1579{
1580 struct ps2dev *ps2dev = &psmouse->ps2dev;
1581
1582 /* 0x0A0 is the command to write the word */
1583 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1584 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1585 alps_monitor_mode_send_word(psmouse, addr) ||
1586 alps_monitor_mode_send_word(psmouse, value) ||
1587 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1588 return -1;
1589
1590 return 0;
1591}
1592
1593static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1594{
1595 struct ps2dev *ps2dev = &psmouse->ps2dev;
1596
1597 if (enable) {
1598 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1599 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1600 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1601 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1602 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1603 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1604 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1605 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1606 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1607 return -1;
1608 } else {
1609 /* EC to exit monitor mode */
1610 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1611 return -1;
1612 }
1613
1614 return 0;
1615}
1616
1617static int alps_absolute_mode_v6(struct psmouse *psmouse)
1618{
1619 u16 reg_val = 0x181;
1620 int ret = -1;
1621
1622 /* enter monitor mode, to write the register */
1623 if (alps_monitor_mode(psmouse, true))
1624 return -1;
1625
1626 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1627
1628 if (alps_monitor_mode(psmouse, false))
1629 ret = -1;
1630
1631 return ret;
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static int alps_get_status(struct psmouse *psmouse, char *param)
1635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001637 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return -1;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return 0;
1641}
1642
1643/*
1644 * Turn touchpad tapping on or off. The sequences are:
1645 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1646 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1647 * My guess that 0xE9 (GetInfo) is here as a sync point.
1648 * For models that also have stickpointer (DualPoints) its tapping
1649 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1650 * we don't fiddle with it.
1651 */
1652static int alps_tap_mode(struct psmouse *psmouse, int enable)
1653{
1654 struct ps2dev *ps2dev = &psmouse->ps2dev;
1655 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1656 unsigned char tap_arg = enable ? 0x0A : 0x00;
1657 unsigned char param[4];
1658
1659 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1660 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1661 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1662 ps2_command(ps2dev, &tap_arg, cmd))
1663 return -1;
1664
1665 if (alps_get_status(psmouse, param))
1666 return -1;
1667
1668 return 0;
1669}
1670
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001671/*
1672 * alps_poll() - poll the touchpad for current motion packet.
1673 * Used in resync.
1674 */
1675static int alps_poll(struct psmouse *psmouse)
1676{
1677 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001678 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001679 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001680
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001681 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001682 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001683
1684 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1685 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1686
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001687 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001688 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001689
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001690 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001691 return -1;
1692
1693 if ((psmouse->badbyte & 0xc8) == 0x08) {
1694/*
1695 * Poll the track stick ...
1696 */
1697 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1698 return -1;
1699 }
1700
1701 memcpy(psmouse->packet, buf, sizeof(buf));
1702 return 0;
1703}
1704
Seth Forshee25bded72011-11-07 19:53:36 -08001705static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001709 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001710 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001714 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001715 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Seth Forshee25bded72011-11-07 19:53:36 -08001719 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001720 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -1;
1722 }
1723
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001724 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001725 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001729 /* ALPS needs stream mode, otherwise it won't report any data */
1730 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001731 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001732 return -1;
1733 }
1734
1735 return 0;
1736}
1737
Yunkang Tang95f75e92013-12-01 22:33:52 -08001738static int alps_hw_init_v6(struct psmouse *psmouse)
1739{
1740 unsigned char param[2] = {0xC8, 0x14};
1741
1742 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1743 if (alps_passthrough_mode_v2(psmouse, true))
1744 return -1;
1745
1746 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1747 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1748 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1749 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1750 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1751 return -1;
1752
1753 if (alps_passthrough_mode_v2(psmouse, false))
1754 return -1;
1755
1756 if (alps_absolute_mode_v6(psmouse)) {
1757 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1758 return -1;
1759 }
1760
1761 return 0;
1762}
1763
Seth Forshee25bded72011-11-07 19:53:36 -08001764/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001765 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001766 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001767static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1768 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001769{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001770 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001771
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001772 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001773 return -1;
1774
Kevin Cernekeecd401202013-02-13 22:28:07 -08001775 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1776 if (reg_val == -1)
1777 goto error;
1778
Seth Forshee25bded72011-11-07 19:53:36 -08001779 if (enable)
1780 reg_val |= 0x01;
1781 else
1782 reg_val &= ~0x01;
1783
Kevin Cernekeecd401202013-02-13 22:28:07 -08001784 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001785
Kevin Cernekeecd401202013-02-13 22:28:07 -08001786error:
1787 if (alps_exit_command_mode(psmouse))
1788 ret = -1;
1789 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001790}
1791
1792/* Must be in command mode when calling this function */
1793static int alps_absolute_mode_v3(struct psmouse *psmouse)
1794{
1795 int reg_val;
1796
1797 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1798 if (reg_val == -1)
1799 return -1;
1800
1801 reg_val |= 0x06;
1802 if (__alps_command_mode_write_reg(psmouse, reg_val))
1803 return -1;
1804
1805 return 0;
1806}
1807
Kevin Cernekeecd401202013-02-13 22:28:07 -08001808static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1809{
1810 int ret = -EIO, reg_val;
1811
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001812 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001813 goto error;
1814
1815 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1816 if (reg_val == -1)
1817 goto error;
1818
1819 /* bit 7: trackstick is present */
1820 ret = reg_val & 0x80 ? 0 : -ENODEV;
1821
1822error:
1823 alps_exit_command_mode(psmouse);
1824 return ret;
1825}
1826
1827static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1828{
1829 struct ps2dev *ps2dev = &psmouse->ps2dev;
1830 int ret = 0;
1831 unsigned char param[4];
1832
1833 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1834 return -EIO;
1835
1836 /*
1837 * E7 report for the trackstick
1838 *
1839 * There have been reports of failures to seem to trace back
1840 * to the above trackstick check failing. When these occur
1841 * this E7 report fails, so when that happens we continue
1842 * with the assumption that there isn't a trackstick after
1843 * all.
1844 */
1845 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08001846 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08001847 ret = -ENODEV;
1848 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001849 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001850
1851 /*
1852 * Not sure what this does, but it is absolutely
1853 * essential. Without it, the touchpad does not
1854 * work at all and the trackstick just emits normal
1855 * PS/2 packets.
1856 */
1857 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1858 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1859 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1860 alps_command_mode_send_nibble(psmouse, 0x9) ||
1861 alps_command_mode_send_nibble(psmouse, 0x4)) {
1862 psmouse_err(psmouse,
1863 "Error sending magic E6 sequence\n");
1864 ret = -EIO;
1865 goto error;
1866 }
1867
1868 /*
1869 * This ensures the trackstick packets are in the format
1870 * supported by this driver. If bit 1 isn't set the packet
1871 * format is different.
1872 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001873 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001874 alps_command_mode_write_reg(psmouse,
1875 reg_base + 0x08, 0x82) ||
1876 alps_exit_command_mode(psmouse))
1877 ret = -EIO;
1878 }
1879
1880error:
1881 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1882 ret = -EIO;
1883
1884 return ret;
1885}
1886
Seth Forshee25bded72011-11-07 19:53:36 -08001887static int alps_hw_init_v3(struct psmouse *psmouse)
1888{
Seth Forshee25bded72011-11-07 19:53:36 -08001889 struct ps2dev *ps2dev = &psmouse->ps2dev;
1890 int reg_val;
1891 unsigned char param[4];
1892
Kevin Cernekeecd401202013-02-13 22:28:07 -08001893 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1894 if (reg_val == -EIO)
1895 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001896
Kevin Cernekeecd401202013-02-13 22:28:07 -08001897 if (reg_val == 0 &&
1898 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001899 goto error;
1900
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001901 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001902 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001903 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1904 goto error;
1905 }
1906
1907 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1908 if (reg_val == -1)
1909 goto error;
1910 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1911 goto error;
1912
1913 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1914 if (reg_val == -1)
1915 goto error;
1916 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1917 goto error;
1918
1919 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1920 goto error;
1921 if (__alps_command_mode_write_reg(psmouse, 0x04))
1922 goto error;
1923
1924 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1925 goto error;
1926 if (__alps_command_mode_write_reg(psmouse, 0x03))
1927 goto error;
1928
1929 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1930 goto error;
1931 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1932 goto error;
1933
1934 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1935 goto error;
1936 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1937 goto error;
1938
Seth Forshee25bded72011-11-07 19:53:36 -08001939 alps_exit_command_mode(psmouse);
1940
1941 /* Set rate and enable data reporting */
1942 param[0] = 0x64;
1943 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1944 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1945 psmouse_err(psmouse, "Failed to enable data reporting\n");
1946 return -1;
1947 }
1948
1949 return 0;
1950
Seth Forshee25bded72011-11-07 19:53:36 -08001951error:
1952 /*
1953 * Leaving the touchpad in command mode will essentially render
1954 * it unusable until the machine reboots, so exit it here just
1955 * to be safe
1956 */
1957 alps_exit_command_mode(psmouse);
1958 return -1;
1959}
1960
Hans de Goedef3f33c62014-07-29 11:22:07 -07001961static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1962{
1963 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1964 struct alps_data *priv = psmouse->private;
1965
1966 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1967 if (reg < 0)
1968 return reg;
1969
1970 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1971 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1972
1973 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1974 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1975
1976 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1977 if (reg < 0)
1978 return reg;
1979
1980 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1981 x_electrode = 17 + x_electrode;
1982
1983 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1984 y_electrode = 13 + y_electrode;
1985
1986 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1987 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1988
1989 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1990 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1991
1992 psmouse_dbg(psmouse,
1993 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1994 x_pitch, y_pitch, x_electrode, y_electrode,
1995 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1996
1997 return 0;
1998}
1999
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002000static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2001{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002002 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002003 struct ps2dev *ps2dev = &psmouse->ps2dev;
2004 int reg_val, ret = -1;
2005
Kevin Cernekeecd401202013-02-13 22:28:07 -08002006 if (priv->flags & ALPS_DUALPOINT) {
2007 reg_val = alps_setup_trackstick_v3(psmouse,
2008 ALPS_REG_BASE_RUSHMORE);
2009 if (reg_val == -EIO)
2010 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002011 }
2012
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002013 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002014 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2015 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2016 goto error;
2017
Hans de Goedef3f33c62014-07-29 11:22:07 -07002018 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2019 goto error;
2020
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002021 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2022 if (reg_val == -1)
2023 goto error;
2024 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2025 goto error;
2026
2027 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2028 goto error;
2029
2030 /* enter absolute mode */
2031 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2032 if (reg_val == -1)
2033 goto error;
2034 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2035 goto error;
2036
2037 alps_exit_command_mode(psmouse);
2038 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2039
2040error:
2041 alps_exit_command_mode(psmouse);
2042 return ret;
2043}
2044
Seth Forshee25bded72011-11-07 19:53:36 -08002045/* Must be in command mode when calling this function */
2046static int alps_absolute_mode_v4(struct psmouse *psmouse)
2047{
2048 int reg_val;
2049
2050 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2051 if (reg_val == -1)
2052 return -1;
2053
2054 reg_val |= 0x02;
2055 if (__alps_command_mode_write_reg(psmouse, reg_val))
2056 return -1;
2057
2058 return 0;
2059}
2060
2061static int alps_hw_init_v4(struct psmouse *psmouse)
2062{
Seth Forshee25bded72011-11-07 19:53:36 -08002063 struct ps2dev *ps2dev = &psmouse->ps2dev;
2064 unsigned char param[4];
2065
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002066 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002067 goto error;
2068
2069 if (alps_absolute_mode_v4(psmouse)) {
2070 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2071 goto error;
2072 }
2073
2074 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2075 goto error;
2076
2077 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2078 goto error;
2079
2080 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2081 goto error;
2082
2083 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2084 goto error;
2085
2086 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2087 goto error;
2088
2089 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2090 goto error;
2091
2092 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2093 goto error;
2094
2095 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2096 goto error;
2097
2098 alps_exit_command_mode(psmouse);
2099
2100 /*
2101 * This sequence changes the output from a 9-byte to an
2102 * 8-byte format. All the same data seems to be present,
2103 * just in a more compact format.
2104 */
2105 param[0] = 0xc8;
2106 param[1] = 0x64;
2107 param[2] = 0x50;
2108 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2109 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2110 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2111 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2112 return -1;
2113
2114 /* Set rate and enable data reporting */
2115 param[0] = 0x64;
2116 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2117 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2118 psmouse_err(psmouse, "Failed to enable data reporting\n");
2119 return -1;
2120 }
2121
2122 return 0;
2123
2124error:
2125 /*
2126 * Leaving the touchpad in command mode will essentially render
2127 * it unusable until the machine reboots, so exit it here just
2128 * to be safe
2129 */
2130 alps_exit_command_mode(psmouse);
2131 return -1;
2132}
2133
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002134static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2135 struct alps_data *priv)
2136{
2137 struct ps2dev *ps2dev = &psmouse->ps2dev;
2138 unsigned char param[4] = {0};
2139 int num_x_electrode, num_y_electrode;
2140
2141 if (alps_enter_command_mode(psmouse))
2142 return -1;
2143
2144 param[0] = 0x0a;
2145 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2146 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2147 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2148 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2149 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2150 return -1;
2151
2152 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2153 return -1;
2154
2155 /*
2156 * Dolphin's sensor line number is not fixed. It can be calculated
2157 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2158 * Further more, we can get device's x_max and y_max by multiplying
2159 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2160 *
2161 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2162 * real sensor line number X = 11 + 8 = 19, and
2163 * real sensor line number Y = 8 + 1 = 9.
2164 * So, x_max = (19 - 1) * 64 = 1152, and
2165 * y_max = (9 - 1) * 64 = 512.
2166 */
2167 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2168 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2169 priv->x_bits = num_x_electrode;
2170 priv->y_bits = num_y_electrode;
2171 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2172 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2173
2174 if (alps_exit_command_mode(psmouse))
2175 return -1;
2176
2177 return 0;
2178}
2179
Dave Turvene75af9e52013-02-21 22:58:28 -08002180static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2181{
2182 struct ps2dev *ps2dev = &psmouse->ps2dev;
2183 unsigned char param[2];
2184
2185 /* This is dolphin "v1" as empirically defined by florin9doi */
2186 param[0] = 0x64;
2187 param[1] = 0x28;
2188
2189 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2190 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2191 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2192 return -1;
2193
2194 return 0;
2195}
2196
Yunkang Tang38088432014-07-26 13:51:41 -07002197static int alps_hw_init_v7(struct psmouse *psmouse)
2198{
2199 struct ps2dev *ps2dev = &psmouse->ps2dev;
2200 int reg_val, ret = -1;
2201
2202 if (alps_enter_command_mode(psmouse) ||
2203 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2204 goto error;
2205
Hans de Goedef3f33c62014-07-29 11:22:07 -07002206 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2207 goto error;
2208
Yunkang Tang38088432014-07-26 13:51:41 -07002209 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2210 goto error;
2211
2212 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2213 if (reg_val == -1)
2214 goto error;
2215 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2216 goto error;
2217
2218 alps_exit_command_mode(psmouse);
2219 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2220
2221error:
2222 alps_exit_command_mode(psmouse);
2223 return ret;
2224}
2225
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002226static int alps_set_protocol(struct psmouse *psmouse,
2227 struct alps_data *priv,
2228 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002229{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002230 psmouse->private = priv;
2231
2232 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2233
2234 priv->proto_version = protocol->version;
2235 priv->byte0 = protocol->byte0;
2236 priv->mask0 = protocol->mask0;
2237 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002238
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002239 priv->x_max = 2000;
2240 priv->y_max = 1400;
2241 priv->x_bits = 15;
2242 priv->y_bits = 11;
2243
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002244 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002245 case ALPS_PROTO_V1:
2246 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002247 priv->hw_init = alps_hw_init_v1_v2;
2248 priv->process_packet = alps_process_packet_v1_v2;
2249 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002250 priv->x_max = 1023;
2251 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002252 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002253
Seth Forshee25bded72011-11-07 19:53:36 -08002254 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002255 priv->hw_init = alps_hw_init_v3;
2256 priv->process_packet = alps_process_packet_v3;
2257 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002258 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002259 priv->nibble_commands = alps_v3_nibble_commands;
2260 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002261 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002262
2263 case ALPS_PROTO_V3_RUSHMORE:
2264 priv->hw_init = alps_hw_init_rushmore_v3;
2265 priv->process_packet = alps_process_packet_v3;
2266 priv->set_abs_params = alps_set_abs_params_mt;
2267 priv->decode_fields = alps_decode_rushmore;
2268 priv->nibble_commands = alps_v3_nibble_commands;
2269 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2270 priv->x_bits = 16;
2271 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002272
2273 if (alps_probe_trackstick_v3(psmouse,
2274 ALPS_REG_BASE_RUSHMORE) < 0)
2275 priv->flags &= ~ALPS_DUALPOINT;
2276
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002277 break;
2278
Seth Forshee25bded72011-11-07 19:53:36 -08002279 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002280 priv->hw_init = alps_hw_init_v4;
2281 priv->process_packet = alps_process_packet_v4;
2282 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002283 priv->nibble_commands = alps_v4_nibble_commands;
2284 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002285 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002286
Dave Turvene75af9e52013-02-21 22:58:28 -08002287 case ALPS_PROTO_V5:
2288 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002289 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002290 priv->decode_fields = alps_decode_dolphin;
2291 priv->set_abs_params = alps_set_abs_params_mt;
2292 priv->nibble_commands = alps_v3_nibble_commands;
2293 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002294 priv->x_bits = 23;
2295 priv->y_bits = 12;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002296
2297 if (alps_dolphin_get_device_area(psmouse, priv))
2298 return -EIO;
2299
Dave Turvene75af9e52013-02-21 22:58:28 -08002300 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002301
Yunkang Tang95f75e92013-12-01 22:33:52 -08002302 case ALPS_PROTO_V6:
2303 priv->hw_init = alps_hw_init_v6;
2304 priv->process_packet = alps_process_packet_v6;
2305 priv->set_abs_params = alps_set_abs_params_st;
2306 priv->nibble_commands = alps_v6_nibble_commands;
2307 priv->x_max = 2047;
2308 priv->y_max = 1535;
2309 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002310
Yunkang Tang38088432014-07-26 13:51:41 -07002311 case ALPS_PROTO_V7:
2312 priv->hw_init = alps_hw_init_v7;
2313 priv->process_packet = alps_process_packet_v7;
2314 priv->decode_fields = alps_decode_packet_v7;
2315 priv->set_abs_params = alps_set_abs_params_mt;
2316 priv->nibble_commands = alps_v3_nibble_commands;
2317 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhovc164c142015-03-21 20:29:34 -07002318 priv->x_max = 0xfff;
2319 priv->y_max = 0x7ff;
Yunkang Tang38088432014-07-26 13:51:41 -07002320
2321 if (priv->fw_ver[1] != 0xba)
2322 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002323
Yunkang Tang38088432014-07-26 13:51:41 -07002324 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002325 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002326
2327 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002328}
2329
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002330static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2331 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002332{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002333 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002334 int i;
2335
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002336 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2337 model = &alps_model_data[i];
2338
2339 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2340 (!model->command_mode_resp ||
2341 model->command_mode_resp == ec[2])) {
2342
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002343 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002344 }
2345 }
2346
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002347 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002348}
2349
2350static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2351{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002352 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002353 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002354 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002355
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002356 /*
2357 * First try "E6 report".
2358 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2359 * The bits 0-2 of the first byte will be 1s if some buttons are
2360 * pressed.
2361 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002362 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2363 PSMOUSE_CMD_SETSCALE11, e6))
2364 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002365
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002366 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2367 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002368
2369 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002370 * Now get the "E7" and "EC" reports. These will uniquely identify
2371 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002372 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002373 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2374 PSMOUSE_CMD_SETSCALE21, e7) ||
2375 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2376 PSMOUSE_CMD_RESET_WRAP, ec) ||
2377 alps_exit_command_mode(psmouse))
2378 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002379
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002380 protocol = alps_match_table(e7, ec);
2381 if (!protocol) {
2382 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2383 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2384 protocol = &alps_v5_protocol_data;
2385 } else if (ec[0] == 0x88 &&
2386 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2387 protocol = &alps_v7_protocol_data;
2388 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2389 protocol = &alps_v3_rushmore_data;
2390 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2391 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2392 protocol = &alps_v3_protocol_data;
2393 } else {
2394 psmouse_dbg(psmouse,
2395 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2396 return -EINVAL;
2397 }
2398 }
2399
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002400 if (priv) {
2401 /* Save the Firmware version */
2402 memcpy(priv->fw_ver, ec, 3);
2403 error = alps_set_protocol(psmouse, priv, protocol);
2404 if (error)
2405 return error;
2406 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002407
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002408 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002409}
2410
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002411static int alps_reconnect(struct psmouse *psmouse)
2412{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002413 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002414
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002415 psmouse_reset(psmouse);
2416
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002417 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002418 return -1;
2419
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002420 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
2423static void alps_disconnect(struct psmouse *psmouse)
2424{
2425 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002428 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002429 if (priv->dev2)
2430 input_unregister_device(priv->dev2);
2431 if (!IS_ERR_OR_NULL(priv->dev3))
2432 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 kfree(priv);
2434}
2435
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002436static void alps_set_abs_params_st(struct alps_data *priv,
2437 struct input_dev *dev1)
2438{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002439 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2440 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002441}
2442
2443static void alps_set_abs_params_mt(struct alps_data *priv,
2444 struct input_dev *dev1)
2445{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002446 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2447 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002448
Hans de Goedef3f33c62014-07-29 11:22:07 -07002449 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2450 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2451
Hans de Goedecdf333e2014-07-25 22:44:42 -07002452 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2453 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2454
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002455 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2456 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002457
2458 /* V7 is real multi-touch */
2459 if (priv->proto_version == ALPS_PROTO_V7)
2460 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002461}
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463int alps_init(struct psmouse *psmouse)
2464{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002465 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002466 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002467 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002469 error = priv->hw_init(psmouse);
2470 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 goto init_fail;
2472
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002473 /*
2474 * Undo part of setup done for us by psmouse core since touchpad
2475 * is not a relative device.
2476 */
2477 __clear_bit(EV_REL, dev1->evbit);
2478 __clear_bit(REL_X, dev1->relbit);
2479 __clear_bit(REL_Y, dev1->relbit);
2480
2481 /*
2482 * Now set up our capabilities.
2483 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002484 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2485 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2486 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002487 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2488 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002490 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002491
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002492 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002493 /* No pressure on V7 */
2494 if (priv->proto_version != ALPS_PROTO_V7)
2495 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002497 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002498 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2499 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002502 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002503 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2504 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002507 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002508 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2509 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2510 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2511 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002512 } else if (priv->flags & ALPS_BUTTONPAD) {
2513 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2514 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002515 } else {
2516 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2517 }
2518
Pali Rohárdfba8602015-01-09 12:48:44 -08002519 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002520 struct input_dev *dev2;
2521
2522 dev2 = input_allocate_device();
2523 if (!dev2) {
2524 psmouse_err(psmouse,
2525 "failed to allocate trackstick device\n");
2526 error = -ENOMEM;
2527 goto init_fail;
2528 }
2529
2530 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2531 psmouse->ps2dev.serio->phys);
2532 dev2->phys = priv->phys2;
2533
Pali Rohárdfba8602015-01-09 12:48:44 -08002534 /*
2535 * format of input device name is: "protocol vendor name"
2536 * see function psmouse_switch_protocol() in psmouse-base.c
2537 */
2538 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002539
2540 dev2->id.bustype = BUS_I8042;
2541 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002542 dev2->id.product = PSMOUSE_ALPS;
2543 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002544 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002545
Pali Rohár04aae282015-01-14 13:18:30 -08002546 input_set_capability(dev2, EV_REL, REL_X);
2547 input_set_capability(dev2, EV_REL, REL_Y);
2548 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2549 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2550 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002551
Pali Rohár04aae282015-01-14 13:18:30 -08002552 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002553 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2554
Pali Rohár04aae282015-01-14 13:18:30 -08002555 error = input_register_device(dev2);
2556 if (error) {
2557 psmouse_err(psmouse,
2558 "failed to register trackstick device: %d\n",
2559 error);
2560 input_free_device(dev2);
2561 goto init_fail;
2562 }
2563
2564 priv->dev2 = dev2;
2565 }
2566
2567 priv->psmouse = psmouse;
2568
2569 INIT_DELAYED_WORK(&priv->dev3_register_work,
2570 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002573 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 psmouse->disconnect = alps_disconnect;
2575 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002576 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002578 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2579 psmouse->resync_time = 0;
2580
Pali Rohár9d720b32014-11-08 12:58:57 -08002581 /* Allow 2 invalid packets without resetting device */
2582 psmouse->resetafter = psmouse->pktsize * 2;
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return 0;
2585
2586init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002587 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002588 /*
2589 * Even though we did not allocate psmouse->private we do free
2590 * it here.
2591 */
2592 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002593 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002594 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002597int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002599 struct alps_data *priv;
2600 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002602 error = alps_identify(psmouse, NULL);
2603 if (error)
2604 return error;
2605
2606 /*
2607 * Reset the device to make sure it is fully operational:
2608 * on some laptops, like certain Dell Latitudes, we may
2609 * fail to properly detect presence of trackstick if device
2610 * has not been reset.
2611 */
2612 psmouse_reset(psmouse);
2613
2614 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2615 if (!priv)
2616 return -ENOMEM;
2617
2618 error = alps_identify(psmouse, priv);
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002619 if (error) {
2620 kfree(priv);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002621 return error;
Dmitry Torokhov93050db2015-02-27 15:49:51 -08002622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 if (set_properties) {
2625 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002626 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002627 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002628 psmouse->model = priv->proto_version;
2629 } else {
2630 /*
2631 * Destroy alps_data structure we allocated earlier since
2632 * this was just a "trial run". Otherwise we'll keep it
2633 * to be used by alps_init() which has to be called if
2634 * we succeed and set_properties is true.
2635 */
2636 kfree(priv);
2637 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return 0;
2641}
2642