blob: a4d69ef0f846fed495e59ea72fcf3483685f035a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Yunkang Tang38088432014-07-26 13:51:41 -0700102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
120 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* XXX */
121 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
122 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800123 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800124 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
125 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
126 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
127 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
128 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
129 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
130 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800133static const struct alps_protocol_info alps_v3_protocol_data = {
134 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
135};
136
137static const struct alps_protocol_info alps_v3_rushmore_data = {
138 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
139};
140
141static const struct alps_protocol_info alps_v5_protocol_data = {
142 ALPS_PROTO_V5, 0xc8, 0xd8, 0
143};
144
145static const struct alps_protocol_info alps_v7_protocol_data = {
146 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
147};
148
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800149static void alps_set_abs_params_st(struct alps_data *priv,
150 struct input_dev *dev1);
151static void alps_set_abs_params_mt(struct alps_data *priv,
152 struct input_dev *dev1);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * XXX - this entry is suspicious. First byte has zero lower nibble,
156 * which is what a normal mouse would report. Also, the value 0x0e
157 * isn't valid per PS/2 spec.
158 */
159
Seth Forsheed4b347b2011-11-07 19:53:15 -0800160/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800162static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800163 unsigned char data)
164{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800165 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800166}
167
Pali Rohár04aae282015-01-14 13:18:30 -0800168static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800169 int left, int right, int middle)
170{
Martin Buckc91ed052010-03-13 22:23:58 -0800171 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800172
Martin Buckc91ed052010-03-13 22:23:58 -0800173 /*
174 * If shared button has already been reported on the
175 * other device (dev2) then this event should be also
176 * sent through that device.
177 */
Pali Rohár04aae282015-01-14 13:18:30 -0800178 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800179 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800180
Pali Rohár04aae282015-01-14 13:18:30 -0800181 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800182 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800183
Pali Rohár04aae282015-01-14 13:18:30 -0800184 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800185 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800186
Martin Buckc91ed052010-03-13 22:23:58 -0800187 /*
188 * Sync the _other_ device now, we'll do the first
189 * device later once we report the rest of the events.
190 */
Pali Rohár04aae282015-01-14 13:18:30 -0800191 if (dev2)
192 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800193}
194
Seth Forshee25bded72011-11-07 19:53:36 -0800195static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct alps_data *priv = psmouse->private;
198 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500199 struct input_dev *dev = psmouse->dev;
200 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500202 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800204 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400205 left = packet[2] & 0x10;
206 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 middle = 0;
208 x = packet[1] | ((packet[0] & 0x07) << 7);
209 y = packet[4] | ((packet[3] & 0x07) << 7);
210 z = packet[5];
211 } else {
212 left = packet[3] & 1;
213 right = packet[3] & 2;
214 middle = packet[3] & 4;
215 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
216 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
217 z = packet[5];
218 }
219
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800220 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400221 back = packet[0] & 0x10;
222 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500223 }
224
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800225 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500226 back = packet[3] & 4;
227 forward = packet[2] & 4;
228 if ((middle = forward && back))
229 forward = back = 0;
230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ges = packet[2] & 1;
233 fin = packet[2] & 2;
234
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800235 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
237 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700238
Pali Rohár04aae282015-01-14 13:18:30 -0800239 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 input_sync(dev2);
242 return;
243 }
244
Pali Rohár04aae282015-01-14 13:18:30 -0800245 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800248 if (ges && !fin)
249 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /*
252 * A "tap and drag" operation is reported by the hardware as a transition
253 * from (!fin && ges) to (fin && ges). This should be translated to the
254 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
255 */
256 if (ges && fin && !priv->prev_fin) {
257 input_report_abs(dev, ABS_X, x);
258 input_report_abs(dev, ABS_Y, y);
259 input_report_abs(dev, ABS_PRESSURE, 0);
260 input_report_key(dev, BTN_TOOL_FINGER, 0);
261 input_sync(dev);
262 }
263 priv->prev_fin = fin;
264
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800265 if (z > 30)
266 input_report_key(dev, BTN_TOUCH, 1);
267 if (z < 25)
268 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (z > 0) {
271 input_report_abs(dev, ABS_X, x);
272 input_report_abs(dev, ABS_Y, y);
273 }
274
275 input_report_abs(dev, ABS_PRESSURE, z);
276 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
277
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800278 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500279 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800281 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500282 input_report_key(dev, BTN_FORWARD, forward);
283 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800286 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800287 input_report_key(dev, BTN_0, packet[2] & 4);
288 input_report_key(dev, BTN_1, packet[0] & 0x10);
289 input_report_key(dev, BTN_2, packet[3] & 4);
290 input_report_key(dev, BTN_3, packet[0] & 0x20);
291 }
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 input_sync(dev);
294}
295
Seth Forshee01ce6612011-11-07 19:54:13 -0800296/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800297 * Process bitmap data for V5 protocols. Return value is null.
298 *
299 * The bitmaps don't have enough data to track fingers, so this function
300 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700301 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800302 */
303static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700304 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800305{
306 int box_middle_x, box_middle_y;
307 unsigned int x_map, y_map;
308 unsigned char start_bit, end_bit;
309 unsigned char x_msb, x_lsb, y_msb, y_lsb;
310
311 x_map = fields->x_map;
312 y_map = fields->y_map;
313
314 if (!x_map || !y_map)
315 return;
316
317 /* Get Most-significant and Least-significant bit */
318 x_msb = fls(x_map);
319 x_lsb = ffs(x_map);
320 y_msb = fls(y_map);
321 y_lsb = ffs(y_map);
322
323 /* Most-significant bit should never exceed max sensor line number */
324 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
325 return;
326
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800327 if (fields->fingers > 1) {
328 start_bit = priv->x_bits - x_msb;
329 end_bit = priv->x_bits - x_lsb;
330 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
331 (2 * (priv->x_bits - 1));
332
333 start_bit = y_lsb - 1;
334 end_bit = y_msb - 1;
335 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
336 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700337 fields->mt[0] = fields->st;
338 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
339 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800340 }
341}
342
Hans de Goede036e6c72014-07-25 22:38:51 -0700343static void alps_get_bitmap_points(unsigned int map,
344 struct alps_bitmap_point *low,
345 struct alps_bitmap_point *high,
346 int *fingers)
347{
348 struct alps_bitmap_point *point;
349 int i, bit, prev_bit = 0;
350
351 point = low;
352 for (i = 0; map != 0; i++, map >>= 1) {
353 bit = map & 1;
354 if (bit) {
355 if (!prev_bit) {
356 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700357 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700358 (*fingers)++;
359 }
360 point->num_bits++;
361 } else {
362 if (prev_bit)
363 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700364 }
365 prev_bit = bit;
366 }
367}
368
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800369/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800370 * Process bitmap data from v3 and v4 protocols. Returns the number of
371 * fingers detected. A return value of 0 means at least one of the
372 * bitmaps was empty.
373 *
374 * The bitmaps don't have enough data to track fingers, so this function
375 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700376 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800377 * is greater than 0.
378 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800379static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700380 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800381{
Hans de Goede036e6c72014-07-25 22:38:51 -0700382 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800383 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
384 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800385
Hans de Goede02d04252014-07-25 22:43:35 -0700386 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800387 return 0;
388
Hans de Goede02d04252014-07-25 22:43:35 -0700389 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
390 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800391
392 /*
393 * Fingers can overlap, so we use the maximum count of fingers
394 * on either axis as the finger count.
395 */
396 fingers = max(fingers_x, fingers_y);
397
398 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700399 * If an axis reports only a single contact, we have overlapping or
400 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800401 */
Hans de Goede20bea682014-07-25 22:33:33 -0700402 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700403 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700404 x_low.num_bits = x_low.num_bits - i;
405 x_high.start_bit = x_low.start_bit + i;
406 x_high.num_bits = max(i, 1);
407 }
408 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700409 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700410 y_low.num_bits = y_low.num_bits - i;
411 y_high.start_bit = y_low.start_bit + i;
412 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800413 }
414
Hans de Goede02d04252014-07-25 22:43:35 -0700415 fields->mt[0].x =
416 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
417 (2 * (priv->x_bits - 1));
418 fields->mt[0].y =
419 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
420 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800421
Hans de Goede02d04252014-07-25 22:43:35 -0700422 fields->mt[1].x =
423 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
424 (2 * (priv->x_bits - 1));
425 fields->mt[1].y =
426 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
427 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800428
Hans de Goede40e8f532014-07-25 22:37:15 -0700429 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800430 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700431 fields->mt[0].y = priv->y_max - fields->mt[0].y;
432 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700433 }
434
Seth Forshee01ce6612011-11-07 19:54:13 -0800435 return fingers;
436}
437
Hans de Goedecdf333e2014-07-25 22:44:42 -0700438static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800439{
440 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700441 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
442 input_report_abs(dev, ABS_MT_POSITION_X, x);
443 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800444}
445
Hans de Goedecdf333e2014-07-25 22:44:42 -0700446static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800447{
Hans de Goede02d04252014-07-25 22:43:35 -0700448 struct alps_data *priv = psmouse->private;
449 struct input_dev *dev = psmouse->dev;
450 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700451 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700452
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800453 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700454 for (i = 0; i < n; i++)
455 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
456
457 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800458}
459
Hans de Goede68c21872014-07-25 22:47:25 -0700460static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
461{
462 struct alps_data *priv = psmouse->private;
463 struct input_dev *dev = psmouse->dev;
464 struct alps_fields *f = &priv->f;
465
466 /* Use st data when we don't have mt data */
467 if (fingers < 2) {
468 f->mt[0].x = f->st.x;
469 f->mt[0].y = f->st.y;
470 fingers = f->pressure > 0 ? 1 : 0;
471 }
472
Hans de Goede99d99962014-07-25 22:48:02 -0700473 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700474
475 input_mt_report_finger_count(dev, fingers);
476
477 input_report_key(dev, BTN_LEFT, f->left);
478 input_report_key(dev, BTN_RIGHT, f->right);
479 input_report_key(dev, BTN_MIDDLE, f->middle);
480
481 input_report_abs(dev, ABS_PRESSURE, f->pressure);
482
483 input_sync(dev);
484}
485
Seth Forshee25bded72011-11-07 19:53:36 -0800486static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
487{
488 struct alps_data *priv = psmouse->private;
489 unsigned char *packet = psmouse->packet;
490 struct input_dev *dev = priv->dev2;
491 int x, y, z, left, right, middle;
492
Pali Rohár34412ba2015-01-09 12:48:58 -0800493 /* It should be a DualPoint when received trackstick packet */
494 if (!(priv->flags & ALPS_DUALPOINT)) {
495 psmouse_warn(psmouse,
496 "Rejected trackstick packet from non DualPoint device");
497 return;
498 }
499
Seth Forshee25bded72011-11-07 19:53:36 -0800500 /* Sanity check packet */
501 if (!(packet[0] & 0x40)) {
502 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
503 return;
504 }
505
506 /*
507 * There's a special packet that seems to indicate the end
508 * of a stream of trackstick data. Filter these out.
509 */
510 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
511 return;
512
513 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
514 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
515 z = (packet[4] & 0x7c) >> 2;
516
517 /*
518 * The x and y values tend to be quite large, and when used
519 * alone the trackstick is difficult to use. Scale them down
520 * to compensate.
521 */
522 x /= 8;
523 y /= 8;
524
525 input_report_rel(dev, REL_X, x);
526 input_report_rel(dev, REL_Y, -y);
527
528 /*
529 * Most ALPS models report the trackstick buttons in the touchpad
530 * packets, but a few report them here. No reliable way has been
531 * found to differentiate between the models upfront, so we enable
532 * the quirk in response to seeing a button press in the trackstick
533 * packet.
534 */
535 left = packet[3] & 0x01;
536 right = packet[3] & 0x02;
537 middle = packet[3] & 0x04;
538
539 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
540 (left || right || middle))
541 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
542
543 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
544 input_report_key(dev, BTN_LEFT, left);
545 input_report_key(dev, BTN_RIGHT, right);
546 input_report_key(dev, BTN_MIDDLE, middle);
547 }
548
549 input_sync(dev);
550 return;
551}
552
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800553static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
554{
555 f->left = !!(p[3] & 0x01);
556 f->right = !!(p[3] & 0x02);
557 f->middle = !!(p[3] & 0x04);
558
559 f->ts_left = !!(p[3] & 0x10);
560 f->ts_right = !!(p[3] & 0x20);
561 f->ts_middle = !!(p[3] & 0x40);
562}
563
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700564static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800565 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800566{
567 f->first_mp = !!(p[4] & 0x40);
568 f->is_mp = !!(p[0] & 0x40);
569
570 f->fingers = (p[5] & 0x3) + 1;
571 f->x_map = ((p[4] & 0x7e) << 8) |
572 ((p[1] & 0x7f) << 2) |
573 ((p[0] & 0x30) >> 4);
574 f->y_map = ((p[3] & 0x70) << 4) |
575 ((p[2] & 0x7f) << 1) |
576 (p[4] & 0x01);
577
Hans de Goede02d04252014-07-25 22:43:35 -0700578 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800579 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700580 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
581 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800582
583 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700584
585 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800586}
587
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700588static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800589 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800590{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800591 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800592
Yunkang Tangf105e342014-07-25 22:29:24 -0700593 /* Rushmore's packet decode has a bit difference with Pinnacle's */
594 f->is_mp = !!(p[5] & 0x40);
595 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800596 f->x_map |= (p[5] & 0x10) << 11;
597 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700598
599 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800600}
601
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700602static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800603 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800604{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800605 u64 palm_data = 0;
606 struct alps_data *priv = psmouse->private;
607
Dave Turvene75af9e52013-02-21 22:58:28 -0800608 f->first_mp = !!(p[0] & 0x02);
609 f->is_mp = !!(p[0] & 0x20);
610
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800611 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700612 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
613 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
614 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800615 alps_decode_buttons_v3(f, p);
616 } else {
617 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800618 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800619
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800620 palm_data = (p[1] & 0x7f) |
621 ((p[2] & 0x7f) << 7) |
622 ((p[4] & 0x7f) << 14) |
623 ((p[5] & 0x7f) << 21) |
624 ((p[3] & 0x07) << 28) |
625 (((u64)p[3] & 0x70) << 27) |
626 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800627
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800628 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
629 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
630
631 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
632 f->x_map = (palm_data >> priv->y_bits) &
633 (BIT(priv->x_bits) - 1);
634 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700635
636 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800637}
638
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800639static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800640{
641 struct alps_data *priv = psmouse->private;
642 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800643 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700644 struct alps_fields *f = &priv->f;
645 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800646
Hans de Goede02d04252014-07-25 22:43:35 -0700647 memset(f, 0, sizeof(*f));
648
649 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800650
651 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800652 * There's no single feature of touchpad position and bitmap packets
653 * that can be used to distinguish between them. We rely on the fact
654 * that a bitmap packet should always follow a position packet with
655 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800656 */
657 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800658 /*
659 * Sometimes a position packet will indicate a multi-packet
660 * sequence, but then what follows is another position
661 * packet. Check for this, and when it happens process the
662 * position packet as usual.
663 */
Hans de Goede02d04252014-07-25 22:43:35 -0700664 if (f->is_mp) {
665 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800666 if (priv->proto_version == ALPS_PROTO_V3 ||
667 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700668 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700669 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800670
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800671 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700672 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800673 psmouse);
674 } else {
675 /*
676 * Because Dolphin uses position packet's
677 * coordinate data as Pt1 and uses it to
678 * calculate Pt2, so we need to do position
679 * packet decode first.
680 */
Hans de Goede02d04252014-07-25 22:43:35 -0700681 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800682 psmouse);
683
684 /*
685 * Since Dolphin's finger number is reliable,
686 * there is no need to compare with bmap_fn.
687 */
Hans de Goede02d04252014-07-25 22:43:35 -0700688 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800689 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800690 } else {
691 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800692 }
693 }
694
Seth Forshee01ce6612011-11-07 19:54:13 -0800695 /*
696 * Bit 6 of byte 0 is not usually set in position packets. The only
697 * times it seems to be set is in situations where the data is
698 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
699 * this combined with the fact that this bit is useful for filtering
700 * out misidentified bitmap packets, we reject anything with this
701 * bit set.
702 */
Hans de Goede02d04252014-07-25 22:43:35 -0700703 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800704 return;
705
Hans de Goede02d04252014-07-25 22:43:35 -0700706 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800707 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800708 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
709 return;
710 }
711
712 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800713
Seth Forshee25bded72011-11-07 19:53:36 -0800714 /*
715 * Sometimes the hardware sends a single packet with z = 0
716 * in the middle of a stream. Real releases generate packets
717 * with x, y, and z all zero, so these seem to be flukes.
718 * Ignore them.
719 */
Hans de Goede02d04252014-07-25 22:43:35 -0700720 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800721 return;
722
Hans de Goede68c21872014-07-25 22:47:25 -0700723 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800724
Pali Rohár34412ba2015-01-09 12:48:58 -0800725 if ((priv->flags & ALPS_DUALPOINT) &&
726 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700727 input_report_key(dev2, BTN_LEFT, f->ts_left);
728 input_report_key(dev2, BTN_RIGHT, f->ts_right);
729 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800730 input_sync(dev2);
731 }
732}
733
734static void alps_process_packet_v3(struct psmouse *psmouse)
735{
736 unsigned char *packet = psmouse->packet;
737
738 /*
739 * v3 protocol packets come in three types, two representing
740 * touchpad data and one representing trackstick data.
741 * Trackstick packets seem to be distinguished by always
742 * having 0x3f in the last byte. This value has never been
743 * observed in the last byte of either of the other types
744 * of packets.
745 */
746 if (packet[5] == 0x3f) {
747 alps_process_trackstick_packet_v3(psmouse);
748 return;
749 }
750
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800751 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800752}
753
Yunkang Tang95f75e92013-12-01 22:33:52 -0800754static void alps_process_packet_v6(struct psmouse *psmouse)
755{
756 struct alps_data *priv = psmouse->private;
757 unsigned char *packet = psmouse->packet;
758 struct input_dev *dev = psmouse->dev;
759 struct input_dev *dev2 = priv->dev2;
760 int x, y, z, left, right, middle;
761
762 /*
763 * We can use Byte5 to distinguish if the packet is from Touchpad
764 * or Trackpoint.
765 * Touchpad: 0 - 0x7E
766 * Trackpoint: 0x7F
767 */
768 if (packet[5] == 0x7F) {
769 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800770 if (!(priv->flags & ALPS_DUALPOINT)) {
771 psmouse_warn(psmouse,
772 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800773 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800774 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800775
776 /* Trackpoint packet */
777 x = packet[1] | ((packet[3] & 0x20) << 2);
778 y = packet[2] | ((packet[3] & 0x40) << 1);
779 z = packet[4];
780 left = packet[3] & 0x01;
781 right = packet[3] & 0x02;
782 middle = packet[3] & 0x04;
783
784 /* To prevent the cursor jump when finger lifted */
785 if (x == 0x7F && y == 0x7F && z == 0x7F)
786 x = y = z = 0;
787
788 /* Divide 4 since trackpoint's speed is too fast */
789 input_report_rel(dev2, REL_X, (char)x / 4);
790 input_report_rel(dev2, REL_Y, -((char)y / 4));
791
792 input_report_key(dev2, BTN_LEFT, left);
793 input_report_key(dev2, BTN_RIGHT, right);
794 input_report_key(dev2, BTN_MIDDLE, middle);
795
796 input_sync(dev2);
797 return;
798 }
799
800 /* Touchpad packet */
801 x = packet[1] | ((packet[3] & 0x78) << 4);
802 y = packet[2] | ((packet[4] & 0x78) << 4);
803 z = packet[5];
804 left = packet[3] & 0x01;
805 right = packet[3] & 0x02;
806
807 if (z > 30)
808 input_report_key(dev, BTN_TOUCH, 1);
809 if (z < 25)
810 input_report_key(dev, BTN_TOUCH, 0);
811
812 if (z > 0) {
813 input_report_abs(dev, ABS_X, x);
814 input_report_abs(dev, ABS_Y, y);
815 }
816
817 input_report_abs(dev, ABS_PRESSURE, z);
818 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
819
820 /* v6 touchpad does not have middle button */
821 input_report_key(dev, BTN_LEFT, left);
822 input_report_key(dev, BTN_RIGHT, right);
823
824 input_sync(dev);
825}
826
Seth Forshee25bded72011-11-07 19:53:36 -0800827static void alps_process_packet_v4(struct psmouse *psmouse)
828{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700829 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800830 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700831 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700832 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700833
834 /*
835 * v4 has a 6-byte encoding for bitmap data, but this data is
836 * broken up between 3 normal packets. Use priv->multi_packet to
837 * track our position in the bitmap packet.
838 */
839 if (packet[6] & 0x40) {
840 /* sync, reset position */
841 priv->multi_packet = 0;
842 }
843
844 if (WARN_ON_ONCE(priv->multi_packet > 2))
845 return;
846
847 offset = 2 * priv->multi_packet;
848 priv->multi_data[offset] = packet[6];
849 priv->multi_data[offset + 1] = packet[7];
850
851 if (++priv->multi_packet > 2) {
852 priv->multi_packet = 0;
853
Hans de Goede02d04252014-07-25 22:43:35 -0700854 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700855 ((priv->multi_data[3] & 0x60) << 3) |
856 ((priv->multi_data[0] & 0x3f) << 2) |
857 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700858 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700859 ((priv->multi_data[3] & 0x1f) << 5) |
860 (priv->multi_data[1] & 0x1f);
861
Hans de Goede02d04252014-07-25 22:43:35 -0700862 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700863 }
Seth Forshee25bded72011-11-07 19:53:36 -0800864
Andreas Boschb0cfb792014-10-15 10:44:50 -0700865 f->left = !!(packet[4] & 0x01);
866 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800867
Hans de Goede02d04252014-07-25 22:43:35 -0700868 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
869 ((packet[0] & 0x30) >> 4);
870 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
871 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800872
Hans de Goede68c21872014-07-25 22:47:25 -0700873 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800874}
875
Yunkang Tang38088432014-07-26 13:51:41 -0700876static bool alps_is_valid_package_v7(struct psmouse *psmouse)
877{
878 switch (psmouse->pktcnt) {
879 case 3:
880 return (psmouse->packet[2] & 0x40) == 0x40;
881 case 4:
882 return (psmouse->packet[3] & 0x48) == 0x48;
883 case 6:
884 return (psmouse->packet[5] & 0x40) == 0x00;
885 }
886 return true;
887}
888
889static unsigned char alps_get_packet_id_v7(char *byte)
890{
891 unsigned char packet_id;
892
893 if (byte[4] & 0x40)
894 packet_id = V7_PACKET_ID_TWO;
895 else if (byte[4] & 0x01)
896 packet_id = V7_PACKET_ID_MULTI;
897 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
898 packet_id = V7_PACKET_ID_NEW;
899 else if (byte[1] == 0x00 && byte[4] == 0x00)
900 packet_id = V7_PACKET_ID_IDLE;
901 else
902 packet_id = V7_PACKET_ID_UNKNOWN;
903
904 return packet_id;
905}
906
907static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
908 unsigned char *pkt,
909 unsigned char pkt_id)
910{
Hans de Goede27a560b2014-12-18 09:54:50 -0800911 /*
912 * packet-fmt b7 b6 b5 b4 b3 b2 b1 b0
913 * Byte0 TWO & MULTI L 1 R M 1 Y0-2 Y0-1 Y0-0
914 * Byte0 NEW L 1 X1-5 1 1 Y0-2 Y0-1 Y0-0
915 * Byte1 Y0-10 Y0-9 Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
916 * Byte2 X0-11 1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
917 * Byte3 X1-11 1 X0-4 X0-3 1 X0-2 X0-1 X0-0
918 * Byte4 TWO X1-10 TWO X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
919 * Byte4 MULTI X1-10 TWO X1-9 X1-8 X1-7 X1-6 Y1-5 1
920 * Byte4 NEW X1-10 TWO X1-9 X1-8 X1-7 X1-6 0 0
921 * Byte5 TWO & NEW Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
922 * Byte5 MULTI Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 F-1 F-0
923 * L: Left button
924 * R / M: Non-clickpads: Right / Middle button
925 * Clickpads: When > 2 fingers are down, and some fingers
926 * are in the button area, then the 2 coordinates reported
927 * are for fingers outside the button area and these report
928 * extra fingers being present in the right / left button
929 * area. Note these fingers are not added to the F field!
930 * so if a TWO packet is received and R = 1 then there are
931 * 3 fingers down, etc.
932 * TWO: 1: Two touches present, byte 0/4/5 are in TWO fmt
933 * 0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
934 * otherwise byte 0 bit 4 must be set and byte 0/4/5 are
935 * in NEW fmt
936 * F: Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
937 */
938
Yunkang Tang38088432014-07-26 13:51:41 -0700939 mt[0].x = ((pkt[2] & 0x80) << 4);
940 mt[0].x |= ((pkt[2] & 0x3F) << 5);
941 mt[0].x |= ((pkt[3] & 0x30) >> 1);
942 mt[0].x |= (pkt[3] & 0x07);
943 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
944
945 mt[1].x = ((pkt[3] & 0x80) << 4);
946 mt[1].x |= ((pkt[4] & 0x80) << 3);
947 mt[1].x |= ((pkt[4] & 0x3F) << 4);
948 mt[1].y = ((pkt[5] & 0x80) << 3);
949 mt[1].y |= ((pkt[5] & 0x3F) << 4);
950
951 switch (pkt_id) {
952 case V7_PACKET_ID_TWO:
953 mt[1].x &= ~0x000F;
954 mt[1].y |= 0x000F;
955 break;
956
957 case V7_PACKET_ID_MULTI:
958 mt[1].x &= ~0x003F;
959 mt[1].y &= ~0x0020;
960 mt[1].y |= ((pkt[4] & 0x02) << 4);
961 mt[1].y |= 0x001F;
962 break;
963
964 case V7_PACKET_ID_NEW:
965 mt[1].x &= ~0x003F;
966 mt[1].x |= (pkt[0] & 0x20);
967 mt[1].y |= 0x000F;
968 break;
969 }
970
971 mt[0].y = 0x7FF - mt[0].y;
972 mt[1].y = 0x7FF - mt[1].y;
973}
974
975static int alps_get_mt_count(struct input_mt_pos *mt)
976{
Hans de Goede7091c442014-12-18 09:53:34 -0800977 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700978
Hans de Goede7091c442014-12-18 09:53:34 -0800979 for (i = 0; i < MAX_TOUCHES; i++) {
980 if (mt[i].x != 0 || mt[i].y != 0)
981 fingers++;
982 }
Yunkang Tang38088432014-07-26 13:51:41 -0700983
Hans de Goede7091c442014-12-18 09:53:34 -0800984 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700985}
986
987static int alps_decode_packet_v7(struct alps_fields *f,
988 unsigned char *p,
989 struct psmouse *psmouse)
990{
Hans de Goeded27eb792014-12-18 09:55:14 -0800991 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700992 unsigned char pkt_id;
993
994 pkt_id = alps_get_packet_id_v7(p);
995 if (pkt_id == V7_PACKET_ID_IDLE)
996 return 0;
997 if (pkt_id == V7_PACKET_ID_UNKNOWN)
998 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -0800999 /*
1000 * NEW packets are send to indicate a discontinuity in the finger
1001 * coordinate reporting. Specifically a finger may have moved from
1002 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1003 * us.
1004 *
1005 * NEW packets have 3 problems:
1006 * 1) They do not contain middle / right button info (on non clickpads)
1007 * this can be worked around by preserving the old button state
1008 * 2) They do not contain an accurate fingercount, and they are
1009 * typically send when the number of fingers changes. We cannot use
1010 * the old finger count as that may mismatch with the amount of
1011 * touch coordinates we've available in the NEW packet
1012 * 3) Their x data for the second touch is inaccurate leading to
1013 * a possible jump of the x coordinate by 16 units when the first
1014 * non NEW packet comes in
1015 * Since problems 2 & 3 cannot be worked around, just ignore them.
1016 */
1017 if (pkt_id == V7_PACKET_ID_NEW)
1018 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001019
1020 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1021
Yunkang Tang38088432014-07-26 13:51:41 -07001022 if (pkt_id == V7_PACKET_ID_TWO)
1023 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001024 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001025 f->fingers = 3 + (p[5] & 0x03);
1026
Hans de Goeded27eb792014-12-18 09:55:14 -08001027 f->left = (p[0] & 0x80) >> 7;
1028 if (priv->flags & ALPS_BUTTONPAD) {
1029 if (p[0] & 0x20)
1030 f->fingers++;
1031 if (p[0] & 0x10)
1032 f->fingers++;
1033 } else {
1034 f->right = (p[0] & 0x20) >> 5;
1035 f->middle = (p[0] & 0x10) >> 4;
1036 }
1037
Hans de Goede7091c442014-12-18 09:53:34 -08001038 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1039 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1040 f->mt[0].x = f->mt[1].x;
1041 f->mt[0].y = f->mt[1].y;
1042 f->mt[1].x = 0;
1043 f->mt[1].y = 0;
1044 }
1045
Yunkang Tang38088432014-07-26 13:51:41 -07001046 return 0;
1047}
1048
1049static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1050{
1051 struct alps_data *priv = psmouse->private;
1052 unsigned char *packet = psmouse->packet;
1053 struct input_dev *dev2 = priv->dev2;
1054 int x, y, z, left, right, middle;
1055
Pali Rohár34412ba2015-01-09 12:48:58 -08001056 /* It should be a DualPoint when received trackstick packet */
1057 if (!(priv->flags & ALPS_DUALPOINT)) {
1058 psmouse_warn(psmouse,
1059 "Rejected trackstick packet from non DualPoint device");
1060 return;
1061 }
1062
Yunkang Tang38088432014-07-26 13:51:41 -07001063 /*
1064 * b7 b6 b5 b4 b3 b2 b1 b0
1065 * Byte0 0 1 0 0 1 0 0 0
1066 * Byte1 1 1 * * 1 M R L
1067 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
1068 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
1069 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
1070 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
1071 * M / R / L: Middle / Right / Left button
1072 */
1073
1074 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1075 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1076 ((packet[3] & 0x20) << 1);
1077 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1078
1079 left = (packet[1] & 0x01);
1080 right = (packet[1] & 0x02) >> 1;
1081 middle = (packet[1] & 0x04) >> 2;
1082
1083 /* Divide 2 since trackpoint's speed is too fast */
1084 input_report_rel(dev2, REL_X, (char)x / 2);
1085 input_report_rel(dev2, REL_Y, -((char)y / 2));
1086
1087 input_report_key(dev2, BTN_LEFT, left);
1088 input_report_key(dev2, BTN_RIGHT, right);
1089 input_report_key(dev2, BTN_MIDDLE, middle);
1090
1091 input_sync(dev2);
1092}
1093
1094static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1095{
1096 struct alps_data *priv = psmouse->private;
1097 struct input_dev *dev = psmouse->dev;
1098 struct alps_fields *f = &priv->f;
1099
1100 memset(f, 0, sizeof(*f));
1101
1102 if (priv->decode_fields(f, psmouse->packet, psmouse))
1103 return;
1104
1105 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1106
1107 input_mt_report_finger_count(dev, f->fingers);
1108
1109 input_report_key(dev, BTN_LEFT, f->left);
1110 input_report_key(dev, BTN_RIGHT, f->right);
1111 input_report_key(dev, BTN_MIDDLE, f->middle);
1112
1113 input_sync(dev);
1114}
1115
1116static void alps_process_packet_v7(struct psmouse *psmouse)
1117{
1118 unsigned char *packet = psmouse->packet;
1119
1120 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1121 alps_process_trackstick_packet_v7(psmouse);
1122 else
1123 alps_process_touchpad_packet_v7(psmouse);
1124}
1125
Pali Rohár04aae282015-01-14 13:18:30 -08001126static DEFINE_MUTEX(alps_mutex);
1127
1128static void alps_register_bare_ps2_mouse(struct work_struct *work)
1129{
1130 struct alps_data *priv =
1131 container_of(work, struct alps_data, dev3_register_work.work);
1132 struct psmouse *psmouse = priv->psmouse;
1133 struct input_dev *dev3;
1134 int error = 0;
1135
1136 mutex_lock(&alps_mutex);
1137
1138 if (priv->dev3)
1139 goto out;
1140
1141 dev3 = input_allocate_device();
1142 if (!dev3) {
1143 psmouse_err(psmouse, "failed to allocate secondary device\n");
1144 error = -ENOMEM;
1145 goto out;
1146 }
1147
1148 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1149 psmouse->ps2dev.serio->phys,
1150 (priv->dev2 ? "input2" : "input1"));
1151 dev3->phys = priv->phys3;
1152
1153 /*
1154 * format of input device name is: "protocol vendor name"
1155 * see function psmouse_switch_protocol() in psmouse-base.c
1156 */
1157 dev3->name = "PS/2 ALPS Mouse";
1158
1159 dev3->id.bustype = BUS_I8042;
1160 dev3->id.vendor = 0x0002;
1161 dev3->id.product = PSMOUSE_PS2;
1162 dev3->id.version = 0x0000;
1163 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1164
1165 input_set_capability(dev3, EV_REL, REL_X);
1166 input_set_capability(dev3, EV_REL, REL_Y);
1167 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1168 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1169 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1170
1171 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1172
1173 error = input_register_device(dev3);
1174 if (error) {
1175 psmouse_err(psmouse,
1176 "failed to register secondary device: %d\n",
1177 error);
1178 input_free_device(dev3);
1179 goto out;
1180 }
1181
1182 priv->dev3 = dev3;
1183
1184out:
1185 /*
1186 * Save the error code so that we can detect that we
1187 * already tried to create the device.
1188 */
1189 if (error)
1190 priv->dev3 = ERR_PTR(error);
1191
1192 mutex_unlock(&alps_mutex);
1193}
1194
1195static void alps_report_bare_ps2_packet(struct input_dev *dev,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001196 unsigned char packet[],
1197 bool report_buttons)
1198{
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001199 if (report_buttons)
Pali Rohár04aae282015-01-14 13:18:30 -08001200 alps_report_buttons(dev, NULL,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001201 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1202
Pali Rohár04aae282015-01-14 13:18:30 -08001203 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001204 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001205 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001206 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1207
Pali Rohár04aae282015-01-14 13:18:30 -08001208 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001209}
1210
1211static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct alps_data *priv = psmouse->private;
1214
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001215 if (psmouse->pktcnt < 6)
1216 return PSMOUSE_GOOD_DATA;
1217
1218 if (psmouse->pktcnt == 6) {
1219 /*
1220 * Start a timer to flush the packet if it ends up last
1221 * 6-byte packet in the stream. Timer needs to fire
1222 * psmouse core times out itself. 20 ms should be enough
1223 * to decide if we are getting more data or not.
1224 */
1225 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1226 return PSMOUSE_GOOD_DATA;
1227 }
1228
1229 del_timer(&priv->timer);
1230
1231 if (psmouse->packet[6] & 0x80) {
1232
1233 /*
1234 * Highest bit is set - that means we either had
1235 * complete ALPS packet and this is start of the
1236 * next packet or we got garbage.
1237 */
1238
1239 if (((psmouse->packet[3] |
1240 psmouse->packet[4] |
1241 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001242 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001243 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001244 "refusing packet %4ph (suspected interleaved ps/2)\n",
1245 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001246 return PSMOUSE_BAD_DATA;
1247 }
1248
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001249 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001250
1251 /* Continue with the next packet */
1252 psmouse->packet[0] = psmouse->packet[6];
1253 psmouse->pktcnt = 1;
1254
1255 } else {
1256
1257 /*
1258 * High bit is 0 - that means that we indeed got a PS/2
1259 * packet in the middle of ALPS packet.
1260 *
1261 * There is also possibility that we got 6-byte ALPS
1262 * packet followed by 3-byte packet from trackpoint. We
1263 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001264 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001265 * normal operation (user would need to press all
1266 * buttons on the pad and start moving trackpoint
1267 * without touching the pad surface) we assume former.
1268 * Even if we are wrong the wost thing that would happen
1269 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001270 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001271 */
1272
Pali Rohár04aae282015-01-14 13:18:30 -08001273 alps_report_bare_ps2_packet(priv->dev2,
1274 &psmouse->packet[3], false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001275
1276 /*
1277 * Continue with the standard ALPS protocol handling,
1278 * but make sure we won't process it as an interleaved
1279 * packet again, which may happen if all buttons are
1280 * pressed. To avoid this let's reset the 4th bit which
1281 * is normally 1.
1282 */
1283 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1284 psmouse->pktcnt = 4;
1285 }
1286
1287 return PSMOUSE_GOOD_DATA;
1288}
1289
1290static void alps_flush_packet(unsigned long data)
1291{
1292 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001293 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001294
1295 serio_pause_rx(psmouse->ps2dev.serio);
1296
Seth Forsheeb46615f2011-11-07 19:53:30 -08001297 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001298
1299 /*
1300 * We did not any more data in reasonable amount of time.
1301 * Validate the last 3 bytes and process as a standard
1302 * ALPS packet.
1303 */
1304 if ((psmouse->packet[3] |
1305 psmouse->packet[4] |
1306 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001307 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001308 "refusing packet %3ph (suspected interleaved ps/2)\n",
1309 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001310 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001311 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001312 }
1313 psmouse->pktcnt = 0;
1314 }
1315
1316 serio_continue_rx(psmouse->ps2dev.serio);
1317}
1318
1319static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1320{
1321 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001322
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001323 /*
1324 * Check if we are dealing with a bare PS/2 packet, presumably from
1325 * a device connected to the external PS/2 port. Because bare PS/2
1326 * protocol does not have enough constant bits to self-synchronize
1327 * properly we only do this if the device is fully synchronized.
1328 */
1329 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
Pali Rohár04aae282015-01-14 13:18:30 -08001330
1331 /* Register dev3 mouse if we received PS/2 packet first time */
1332 if (unlikely(!priv->dev3))
1333 psmouse_queue_work(psmouse,
1334 &priv->dev3_register_work, 0);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (psmouse->pktcnt == 3) {
Pali Rohár04aae282015-01-14 13:18:30 -08001337 /* Once dev3 mouse device is registered report data */
1338 if (likely(!IS_ERR_OR_NULL(priv->dev3)))
1339 alps_report_bare_ps2_packet(priv->dev3,
1340 psmouse->packet,
1341 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return PSMOUSE_FULL_PACKET;
1343 }
1344 return PSMOUSE_GOOD_DATA;
1345 }
1346
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001347 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1348
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001349 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001350 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1351 return alps_handle_interleaved_ps2(psmouse);
1352 }
1353
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001354 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001355 psmouse_dbg(psmouse,
1356 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001357 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Seth Forsheeb46615f2011-11-07 19:53:30 -08001361 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001362 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001363 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001364 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001365 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1366 psmouse->pktcnt - 1,
1367 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001368
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001369 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001370 psmouse->pktcnt == psmouse->pktsize) {
1371 /*
1372 * Some Dell boxes, such as Latitude E6440 or E7440
1373 * with closed lid, quite often smash last byte of
1374 * otherwise valid packet with 0xff. Given that the
1375 * next packet is very likely to be valid let's
1376 * report PSMOUSE_FULL_PACKET but not process data,
1377 * rather than reporting PSMOUSE_BAD_DATA and
1378 * filling the logs.
1379 */
1380 return PSMOUSE_FULL_PACKET;
1381 }
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Yunkang Tang38088432014-07-26 13:51:41 -07001386 if (priv->proto_version == ALPS_PROTO_V7 &&
1387 !alps_is_valid_package_v7(psmouse)) {
1388 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1389 psmouse->pktcnt - 1,
1390 psmouse->packet[psmouse->pktcnt - 1]);
1391 return PSMOUSE_BAD_DATA;
1392 }
1393
Seth Forsheeb46615f2011-11-07 19:53:30 -08001394 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001395 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return PSMOUSE_FULL_PACKET;
1397 }
1398
1399 return PSMOUSE_GOOD_DATA;
1400}
1401
Seth Forshee25bded72011-11-07 19:53:36 -08001402static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1403{
1404 struct ps2dev *ps2dev = &psmouse->ps2dev;
1405 struct alps_data *priv = psmouse->private;
1406 int command;
1407 unsigned char *param;
1408 unsigned char dummy[4];
1409
1410 BUG_ON(nibble > 0xf);
1411
1412 command = priv->nibble_commands[nibble].command;
1413 param = (command & 0x0f00) ?
1414 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1415
1416 if (ps2_command(ps2dev, param, command))
1417 return -1;
1418
1419 return 0;
1420}
1421
1422static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1423{
1424 struct ps2dev *ps2dev = &psmouse->ps2dev;
1425 struct alps_data *priv = psmouse->private;
1426 int i, nibble;
1427
1428 if (ps2_command(ps2dev, NULL, priv->addr_command))
1429 return -1;
1430
1431 for (i = 12; i >= 0; i -= 4) {
1432 nibble = (addr >> i) & 0xf;
1433 if (alps_command_mode_send_nibble(psmouse, nibble))
1434 return -1;
1435 }
1436
1437 return 0;
1438}
1439
1440static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1441{
1442 struct ps2dev *ps2dev = &psmouse->ps2dev;
1443 unsigned char param[4];
1444
1445 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1446 return -1;
1447
1448 /*
1449 * The address being read is returned in the first two bytes
1450 * of the result. Check that this address matches the expected
1451 * address.
1452 */
1453 if (addr != ((param[0] << 8) | param[1]))
1454 return -1;
1455
1456 return param[2];
1457}
1458
1459static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1460{
1461 if (alps_command_mode_set_addr(psmouse, addr))
1462 return -1;
1463 return __alps_command_mode_read_reg(psmouse, addr);
1464}
1465
1466static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1467{
1468 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1469 return -1;
1470 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1471 return -1;
1472 return 0;
1473}
1474
1475static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1476 u8 value)
1477{
1478 if (alps_command_mode_set_addr(psmouse, addr))
1479 return -1;
1480 return __alps_command_mode_write_reg(psmouse, value);
1481}
1482
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001483static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1484 int repeated_command, unsigned char *param)
1485{
1486 struct ps2dev *ps2dev = &psmouse->ps2dev;
1487
1488 param[0] = 0;
1489 if (init_command && ps2_command(ps2dev, param, init_command))
1490 return -EIO;
1491
1492 if (ps2_command(ps2dev, NULL, repeated_command) ||
1493 ps2_command(ps2dev, NULL, repeated_command) ||
1494 ps2_command(ps2dev, NULL, repeated_command))
1495 return -EIO;
1496
1497 param[0] = param[1] = param[2] = 0xff;
1498 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1499 return -EIO;
1500
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001501 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1502 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001503 return 0;
1504}
1505
Yunkang Tang38088432014-07-26 13:51:41 -07001506static bool alps_check_valid_firmware_id(unsigned char id[])
1507{
1508 if (id[0] == 0x73)
1509 return true;
1510
1511 if (id[0] == 0x88 &&
1512 (id[1] == 0x07 ||
1513 id[1] == 0x08 ||
1514 (id[1] & 0xf0) == 0xb0 ||
1515 (id[1] & 0xf0) == 0xc0)) {
1516 return true;
1517 }
1518
1519 return false;
1520}
1521
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001522static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001523{
1524 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001525
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001526 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001527 psmouse_err(psmouse, "failed to enter command mode\n");
1528 return -1;
1529 }
1530
Yunkang Tang38088432014-07-26 13:51:41 -07001531 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001532 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001533 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001534 return -1;
1535 }
Seth Forshee25bded72011-11-07 19:53:36 -08001536 return 0;
1537}
1538
1539static inline int alps_exit_command_mode(struct psmouse *psmouse)
1540{
1541 struct ps2dev *ps2dev = &psmouse->ps2dev;
1542 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1543 return -1;
1544 return 0;
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
1548 * For DualPoint devices select the device that should respond to
1549 * subsequent commands. It looks like glidepad is behind stickpointer,
1550 * I'd thought it would be other way around...
1551 */
Seth Forshee25bded72011-11-07 19:53:36 -08001552static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1556
1557 if (ps2_command(ps2dev, NULL, cmd) ||
1558 ps2_command(ps2dev, NULL, cmd) ||
1559 ps2_command(ps2dev, NULL, cmd) ||
1560 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1561 return -1;
1562
1563 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001564 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 return 0;
1567}
1568
Seth Forshee25bded72011-11-07 19:53:36 -08001569static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct ps2dev *ps2dev = &psmouse->ps2dev;
1572
1573 /* Try ALPS magic knock - 4 disable before enable */
1574 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1575 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1576 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1577 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1578 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1579 return -1;
1580
1581 /*
1582 * Switch mouse to poll (remote) mode so motion data will not
1583 * get in our way
1584 */
1585 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1586}
1587
Yunkang Tang95f75e92013-12-01 22:33:52 -08001588static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1589{
1590 int i, nibble;
1591
1592 /*
1593 * b0-b11 are valid bits, send sequence is inverse.
1594 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1595 */
1596 for (i = 0; i <= 8; i += 4) {
1597 nibble = (word >> i) & 0xf;
1598 if (alps_command_mode_send_nibble(psmouse, nibble))
1599 return -1;
1600 }
1601
1602 return 0;
1603}
1604
1605static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1606 u16 addr, u16 value)
1607{
1608 struct ps2dev *ps2dev = &psmouse->ps2dev;
1609
1610 /* 0x0A0 is the command to write the word */
1611 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1612 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1613 alps_monitor_mode_send_word(psmouse, addr) ||
1614 alps_monitor_mode_send_word(psmouse, value) ||
1615 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1616 return -1;
1617
1618 return 0;
1619}
1620
1621static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1622{
1623 struct ps2dev *ps2dev = &psmouse->ps2dev;
1624
1625 if (enable) {
1626 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1627 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1628 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1629 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1630 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1631 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1632 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1633 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1634 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1635 return -1;
1636 } else {
1637 /* EC to exit monitor mode */
1638 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1639 return -1;
1640 }
1641
1642 return 0;
1643}
1644
1645static int alps_absolute_mode_v6(struct psmouse *psmouse)
1646{
1647 u16 reg_val = 0x181;
1648 int ret = -1;
1649
1650 /* enter monitor mode, to write the register */
1651 if (alps_monitor_mode(psmouse, true))
1652 return -1;
1653
1654 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1655
1656 if (alps_monitor_mode(psmouse, false))
1657 ret = -1;
1658
1659 return ret;
1660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static int alps_get_status(struct psmouse *psmouse, char *param)
1663{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001665 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -1;
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669}
1670
1671/*
1672 * Turn touchpad tapping on or off. The sequences are:
1673 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1674 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1675 * My guess that 0xE9 (GetInfo) is here as a sync point.
1676 * For models that also have stickpointer (DualPoints) its tapping
1677 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1678 * we don't fiddle with it.
1679 */
1680static int alps_tap_mode(struct psmouse *psmouse, int enable)
1681{
1682 struct ps2dev *ps2dev = &psmouse->ps2dev;
1683 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1684 unsigned char tap_arg = enable ? 0x0A : 0x00;
1685 unsigned char param[4];
1686
1687 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1688 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1689 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1690 ps2_command(ps2dev, &tap_arg, cmd))
1691 return -1;
1692
1693 if (alps_get_status(psmouse, param))
1694 return -1;
1695
1696 return 0;
1697}
1698
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001699/*
1700 * alps_poll() - poll the touchpad for current motion packet.
1701 * Used in resync.
1702 */
1703static int alps_poll(struct psmouse *psmouse)
1704{
1705 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001706 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001707 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001708
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);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001711
1712 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1713 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1714
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001715 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001716 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001717
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001718 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001719 return -1;
1720
1721 if ((psmouse->badbyte & 0xc8) == 0x08) {
1722/*
1723 * Poll the track stick ...
1724 */
1725 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1726 return -1;
1727 }
1728
1729 memcpy(psmouse->packet, buf, sizeof(buf));
1730 return 0;
1731}
1732
Seth Forshee25bded72011-11-07 19:53:36 -08001733static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001737 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001738 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001742 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001743 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Seth Forshee25bded72011-11-07 19:53:36 -08001747 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001748 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return -1;
1750 }
1751
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001752 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001753 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001757 /* ALPS needs stream mode, otherwise it won't report any data */
1758 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001759 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001760 return -1;
1761 }
1762
1763 return 0;
1764}
1765
Yunkang Tang95f75e92013-12-01 22:33:52 -08001766static int alps_hw_init_v6(struct psmouse *psmouse)
1767{
1768 unsigned char param[2] = {0xC8, 0x14};
1769
1770 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1771 if (alps_passthrough_mode_v2(psmouse, true))
1772 return -1;
1773
1774 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1775 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1776 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1777 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1778 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1779 return -1;
1780
1781 if (alps_passthrough_mode_v2(psmouse, false))
1782 return -1;
1783
1784 if (alps_absolute_mode_v6(psmouse)) {
1785 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1786 return -1;
1787 }
1788
1789 return 0;
1790}
1791
Seth Forshee25bded72011-11-07 19:53:36 -08001792/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001793 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001794 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001795static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1796 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001797{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001798 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001799
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001800 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001801 return -1;
1802
Kevin Cernekeecd401202013-02-13 22:28:07 -08001803 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1804 if (reg_val == -1)
1805 goto error;
1806
Seth Forshee25bded72011-11-07 19:53:36 -08001807 if (enable)
1808 reg_val |= 0x01;
1809 else
1810 reg_val &= ~0x01;
1811
Kevin Cernekeecd401202013-02-13 22:28:07 -08001812 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001813
Kevin Cernekeecd401202013-02-13 22:28:07 -08001814error:
1815 if (alps_exit_command_mode(psmouse))
1816 ret = -1;
1817 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001818}
1819
1820/* Must be in command mode when calling this function */
1821static int alps_absolute_mode_v3(struct psmouse *psmouse)
1822{
1823 int reg_val;
1824
1825 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1826 if (reg_val == -1)
1827 return -1;
1828
1829 reg_val |= 0x06;
1830 if (__alps_command_mode_write_reg(psmouse, reg_val))
1831 return -1;
1832
1833 return 0;
1834}
1835
Kevin Cernekeecd401202013-02-13 22:28:07 -08001836static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1837{
1838 int ret = -EIO, reg_val;
1839
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001840 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001841 goto error;
1842
1843 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1844 if (reg_val == -1)
1845 goto error;
1846
1847 /* bit 7: trackstick is present */
1848 ret = reg_val & 0x80 ? 0 : -ENODEV;
1849
1850error:
1851 alps_exit_command_mode(psmouse);
1852 return ret;
1853}
1854
1855static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1856{
1857 struct ps2dev *ps2dev = &psmouse->ps2dev;
1858 int ret = 0;
1859 unsigned char param[4];
1860
1861 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1862 return -EIO;
1863
1864 /*
1865 * E7 report for the trackstick
1866 *
1867 * There have been reports of failures to seem to trace back
1868 * to the above trackstick check failing. When these occur
1869 * this E7 report fails, so when that happens we continue
1870 * with the assumption that there isn't a trackstick after
1871 * all.
1872 */
1873 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08001874 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08001875 ret = -ENODEV;
1876 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001877 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001878
1879 /*
1880 * Not sure what this does, but it is absolutely
1881 * essential. Without it, the touchpad does not
1882 * work at all and the trackstick just emits normal
1883 * PS/2 packets.
1884 */
1885 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1886 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1887 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1888 alps_command_mode_send_nibble(psmouse, 0x9) ||
1889 alps_command_mode_send_nibble(psmouse, 0x4)) {
1890 psmouse_err(psmouse,
1891 "Error sending magic E6 sequence\n");
1892 ret = -EIO;
1893 goto error;
1894 }
1895
1896 /*
1897 * This ensures the trackstick packets are in the format
1898 * supported by this driver. If bit 1 isn't set the packet
1899 * format is different.
1900 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001901 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001902 alps_command_mode_write_reg(psmouse,
1903 reg_base + 0x08, 0x82) ||
1904 alps_exit_command_mode(psmouse))
1905 ret = -EIO;
1906 }
1907
1908error:
1909 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1910 ret = -EIO;
1911
1912 return ret;
1913}
1914
Seth Forshee25bded72011-11-07 19:53:36 -08001915static int alps_hw_init_v3(struct psmouse *psmouse)
1916{
Seth Forshee25bded72011-11-07 19:53:36 -08001917 struct ps2dev *ps2dev = &psmouse->ps2dev;
1918 int reg_val;
1919 unsigned char param[4];
1920
Kevin Cernekeecd401202013-02-13 22:28:07 -08001921 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1922 if (reg_val == -EIO)
1923 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001924
Kevin Cernekeecd401202013-02-13 22:28:07 -08001925 if (reg_val == 0 &&
1926 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001927 goto error;
1928
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001929 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001930 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001931 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1932 goto error;
1933 }
1934
1935 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1936 if (reg_val == -1)
1937 goto error;
1938 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1939 goto error;
1940
1941 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1942 if (reg_val == -1)
1943 goto error;
1944 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1945 goto error;
1946
1947 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1948 goto error;
1949 if (__alps_command_mode_write_reg(psmouse, 0x04))
1950 goto error;
1951
1952 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1953 goto error;
1954 if (__alps_command_mode_write_reg(psmouse, 0x03))
1955 goto error;
1956
1957 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1958 goto error;
1959 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1960 goto error;
1961
1962 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1963 goto error;
1964 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1965 goto error;
1966
Seth Forshee25bded72011-11-07 19:53:36 -08001967 alps_exit_command_mode(psmouse);
1968
1969 /* Set rate and enable data reporting */
1970 param[0] = 0x64;
1971 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1972 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1973 psmouse_err(psmouse, "Failed to enable data reporting\n");
1974 return -1;
1975 }
1976
1977 return 0;
1978
Seth Forshee25bded72011-11-07 19:53:36 -08001979error:
1980 /*
1981 * Leaving the touchpad in command mode will essentially render
1982 * it unusable until the machine reboots, so exit it here just
1983 * to be safe
1984 */
1985 alps_exit_command_mode(psmouse);
1986 return -1;
1987}
1988
Hans de Goedef3f33c62014-07-29 11:22:07 -07001989static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1990{
1991 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1992 struct alps_data *priv = psmouse->private;
1993
1994 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1995 if (reg < 0)
1996 return reg;
1997
1998 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1999 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2000
2001 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2002 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2003
2004 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2005 if (reg < 0)
2006 return reg;
2007
2008 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2009 x_electrode = 17 + x_electrode;
2010
2011 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2012 y_electrode = 13 + y_electrode;
2013
2014 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2015 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2016
2017 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2018 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2019
2020 psmouse_dbg(psmouse,
2021 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2022 x_pitch, y_pitch, x_electrode, y_electrode,
2023 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2024
2025 return 0;
2026}
2027
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002028static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2029{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002030 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002031 struct ps2dev *ps2dev = &psmouse->ps2dev;
2032 int reg_val, ret = -1;
2033
Kevin Cernekeecd401202013-02-13 22:28:07 -08002034 if (priv->flags & ALPS_DUALPOINT) {
2035 reg_val = alps_setup_trackstick_v3(psmouse,
2036 ALPS_REG_BASE_RUSHMORE);
2037 if (reg_val == -EIO)
2038 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002039 }
2040
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002041 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002042 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2043 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2044 goto error;
2045
Hans de Goedef3f33c62014-07-29 11:22:07 -07002046 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2047 goto error;
2048
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002049 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2050 if (reg_val == -1)
2051 goto error;
2052 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2053 goto error;
2054
2055 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2056 goto error;
2057
2058 /* enter absolute mode */
2059 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2060 if (reg_val == -1)
2061 goto error;
2062 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2063 goto error;
2064
2065 alps_exit_command_mode(psmouse);
2066 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2067
2068error:
2069 alps_exit_command_mode(psmouse);
2070 return ret;
2071}
2072
Seth Forshee25bded72011-11-07 19:53:36 -08002073/* Must be in command mode when calling this function */
2074static int alps_absolute_mode_v4(struct psmouse *psmouse)
2075{
2076 int reg_val;
2077
2078 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2079 if (reg_val == -1)
2080 return -1;
2081
2082 reg_val |= 0x02;
2083 if (__alps_command_mode_write_reg(psmouse, reg_val))
2084 return -1;
2085
2086 return 0;
2087}
2088
2089static int alps_hw_init_v4(struct psmouse *psmouse)
2090{
Seth Forshee25bded72011-11-07 19:53:36 -08002091 struct ps2dev *ps2dev = &psmouse->ps2dev;
2092 unsigned char param[4];
2093
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002094 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002095 goto error;
2096
2097 if (alps_absolute_mode_v4(psmouse)) {
2098 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2099 goto error;
2100 }
2101
2102 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2103 goto error;
2104
2105 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2106 goto error;
2107
2108 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2109 goto error;
2110
2111 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2112 goto error;
2113
2114 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2115 goto error;
2116
2117 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2118 goto error;
2119
2120 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2121 goto error;
2122
2123 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2124 goto error;
2125
2126 alps_exit_command_mode(psmouse);
2127
2128 /*
2129 * This sequence changes the output from a 9-byte to an
2130 * 8-byte format. All the same data seems to be present,
2131 * just in a more compact format.
2132 */
2133 param[0] = 0xc8;
2134 param[1] = 0x64;
2135 param[2] = 0x50;
2136 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2137 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2138 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2139 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2140 return -1;
2141
2142 /* Set rate and enable data reporting */
2143 param[0] = 0x64;
2144 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2145 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2146 psmouse_err(psmouse, "Failed to enable data reporting\n");
2147 return -1;
2148 }
2149
2150 return 0;
2151
2152error:
2153 /*
2154 * Leaving the touchpad in command mode will essentially render
2155 * it unusable until the machine reboots, so exit it here just
2156 * to be safe
2157 */
2158 alps_exit_command_mode(psmouse);
2159 return -1;
2160}
2161
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002162static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2163 struct alps_data *priv)
2164{
2165 struct ps2dev *ps2dev = &psmouse->ps2dev;
2166 unsigned char param[4] = {0};
2167 int num_x_electrode, num_y_electrode;
2168
2169 if (alps_enter_command_mode(psmouse))
2170 return -1;
2171
2172 param[0] = 0x0a;
2173 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2174 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2175 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2176 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2177 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2178 return -1;
2179
2180 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2181 return -1;
2182
2183 /*
2184 * Dolphin's sensor line number is not fixed. It can be calculated
2185 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2186 * Further more, we can get device's x_max and y_max by multiplying
2187 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2188 *
2189 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2190 * real sensor line number X = 11 + 8 = 19, and
2191 * real sensor line number Y = 8 + 1 = 9.
2192 * So, x_max = (19 - 1) * 64 = 1152, and
2193 * y_max = (9 - 1) * 64 = 512.
2194 */
2195 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2196 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2197 priv->x_bits = num_x_electrode;
2198 priv->y_bits = num_y_electrode;
2199 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2200 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2201
2202 if (alps_exit_command_mode(psmouse))
2203 return -1;
2204
2205 return 0;
2206}
2207
Dave Turvene75af9e52013-02-21 22:58:28 -08002208static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2209{
2210 struct ps2dev *ps2dev = &psmouse->ps2dev;
2211 unsigned char param[2];
2212
2213 /* This is dolphin "v1" as empirically defined by florin9doi */
2214 param[0] = 0x64;
2215 param[1] = 0x28;
2216
2217 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2218 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2219 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2220 return -1;
2221
2222 return 0;
2223}
2224
Yunkang Tang38088432014-07-26 13:51:41 -07002225static int alps_hw_init_v7(struct psmouse *psmouse)
2226{
2227 struct ps2dev *ps2dev = &psmouse->ps2dev;
2228 int reg_val, ret = -1;
2229
2230 if (alps_enter_command_mode(psmouse) ||
2231 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2232 goto error;
2233
Hans de Goedef3f33c62014-07-29 11:22:07 -07002234 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2235 goto error;
2236
Yunkang Tang38088432014-07-26 13:51:41 -07002237 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2238 goto error;
2239
2240 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2241 if (reg_val == -1)
2242 goto error;
2243 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2244 goto error;
2245
2246 alps_exit_command_mode(psmouse);
2247 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2248
2249error:
2250 alps_exit_command_mode(psmouse);
2251 return ret;
2252}
2253
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002254static int alps_set_protocol(struct psmouse *psmouse,
2255 struct alps_data *priv,
2256 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002257{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002258 psmouse->private = priv;
2259
2260 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2261
2262 priv->proto_version = protocol->version;
2263 priv->byte0 = protocol->byte0;
2264 priv->mask0 = protocol->mask0;
2265 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002266
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002267 priv->x_max = 2000;
2268 priv->y_max = 1400;
2269 priv->x_bits = 15;
2270 priv->y_bits = 11;
2271
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002272 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002273 case ALPS_PROTO_V1:
2274 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002275 priv->hw_init = alps_hw_init_v1_v2;
2276 priv->process_packet = alps_process_packet_v1_v2;
2277 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002278 priv->x_max = 1023;
2279 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002280 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002281
Seth Forshee25bded72011-11-07 19:53:36 -08002282 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002283 priv->hw_init = alps_hw_init_v3;
2284 priv->process_packet = alps_process_packet_v3;
2285 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002286 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002287 priv->nibble_commands = alps_v3_nibble_commands;
2288 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002289 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002290
2291 case ALPS_PROTO_V3_RUSHMORE:
2292 priv->hw_init = alps_hw_init_rushmore_v3;
2293 priv->process_packet = alps_process_packet_v3;
2294 priv->set_abs_params = alps_set_abs_params_mt;
2295 priv->decode_fields = alps_decode_rushmore;
2296 priv->nibble_commands = alps_v3_nibble_commands;
2297 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2298 priv->x_bits = 16;
2299 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002300
2301 if (alps_probe_trackstick_v3(psmouse,
2302 ALPS_REG_BASE_RUSHMORE) < 0)
2303 priv->flags &= ~ALPS_DUALPOINT;
2304
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002305 break;
2306
Seth Forshee25bded72011-11-07 19:53:36 -08002307 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002308 priv->hw_init = alps_hw_init_v4;
2309 priv->process_packet = alps_process_packet_v4;
2310 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002311 priv->nibble_commands = alps_v4_nibble_commands;
2312 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002313 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002314
Dave Turvene75af9e52013-02-21 22:58:28 -08002315 case ALPS_PROTO_V5:
2316 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002317 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002318 priv->decode_fields = alps_decode_dolphin;
2319 priv->set_abs_params = alps_set_abs_params_mt;
2320 priv->nibble_commands = alps_v3_nibble_commands;
2321 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002322 priv->x_max = 1360;
2323 priv->y_max = 660;
2324 priv->x_bits = 23;
2325 priv->y_bits = 12;
2326 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002327
Yunkang Tang95f75e92013-12-01 22:33:52 -08002328 case ALPS_PROTO_V6:
2329 priv->hw_init = alps_hw_init_v6;
2330 priv->process_packet = alps_process_packet_v6;
2331 priv->set_abs_params = alps_set_abs_params_st;
2332 priv->nibble_commands = alps_v6_nibble_commands;
2333 priv->x_max = 2047;
2334 priv->y_max = 1535;
2335 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002336
Yunkang Tang38088432014-07-26 13:51:41 -07002337 case ALPS_PROTO_V7:
2338 priv->hw_init = alps_hw_init_v7;
2339 priv->process_packet = alps_process_packet_v7;
2340 priv->decode_fields = alps_decode_packet_v7;
2341 priv->set_abs_params = alps_set_abs_params_mt;
2342 priv->nibble_commands = alps_v3_nibble_commands;
2343 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002344
2345 if (alps_dolphin_get_device_area(psmouse, priv))
2346 return -EIO;
Yunkang Tang38088432014-07-26 13:51:41 -07002347
2348 if (priv->fw_ver[1] != 0xba)
2349 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002350
Yunkang Tang38088432014-07-26 13:51:41 -07002351 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002352 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002353
2354 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002355}
2356
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002357static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2358 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002359{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002360 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002361 int i;
2362
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002363 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2364 model = &alps_model_data[i];
2365
2366 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2367 (!model->command_mode_resp ||
2368 model->command_mode_resp == ec[2])) {
2369
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002370 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002371 }
2372 }
2373
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002374 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002375}
2376
2377static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2378{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002379 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002380 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002381 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002382
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002383 /*
2384 * First try "E6 report".
2385 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2386 * The bits 0-2 of the first byte will be 1s if some buttons are
2387 * pressed.
2388 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002389 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2390 PSMOUSE_CMD_SETSCALE11, e6))
2391 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002392
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002393 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2394 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002395
2396 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002397 * Now get the "E7" and "EC" reports. These will uniquely identify
2398 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002399 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002400 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2401 PSMOUSE_CMD_SETSCALE21, e7) ||
2402 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2403 PSMOUSE_CMD_RESET_WRAP, ec) ||
2404 alps_exit_command_mode(psmouse))
2405 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002406
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002407 protocol = alps_match_table(e7, ec);
2408 if (!protocol) {
2409 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2410 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2411 protocol = &alps_v5_protocol_data;
2412 } else if (ec[0] == 0x88 &&
2413 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2414 protocol = &alps_v7_protocol_data;
2415 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2416 protocol = &alps_v3_rushmore_data;
2417 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2418 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2419 protocol = &alps_v3_protocol_data;
2420 } else {
2421 psmouse_dbg(psmouse,
2422 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2423 return -EINVAL;
2424 }
2425 }
2426
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002427 if (priv) {
2428 /* Save the Firmware version */
2429 memcpy(priv->fw_ver, ec, 3);
2430 error = alps_set_protocol(psmouse, priv, protocol);
2431 if (error)
2432 return error;
2433 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002434
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002435 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002436}
2437
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002438static int alps_reconnect(struct psmouse *psmouse)
2439{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002440 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002441
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002442 psmouse_reset(psmouse);
2443
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002444 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002445 return -1;
2446
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002447 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
2450static void alps_disconnect(struct psmouse *psmouse)
2451{
2452 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002455 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002456 if (priv->dev2)
2457 input_unregister_device(priv->dev2);
2458 if (!IS_ERR_OR_NULL(priv->dev3))
2459 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 kfree(priv);
2461}
2462
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002463static void alps_set_abs_params_st(struct alps_data *priv,
2464 struct input_dev *dev1)
2465{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002466 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2467 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002468}
2469
2470static void alps_set_abs_params_mt(struct alps_data *priv,
2471 struct input_dev *dev1)
2472{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002473 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2474 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002475
Hans de Goedef3f33c62014-07-29 11:22:07 -07002476 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2477 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2478
Hans de Goedecdf333e2014-07-25 22:44:42 -07002479 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2480 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2481
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002482 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2483 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002484
2485 /* V7 is real multi-touch */
2486 if (priv->proto_version == ALPS_PROTO_V7)
2487 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002488}
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490int alps_init(struct psmouse *psmouse)
2491{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002492 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002493 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002494 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002496 error = priv->hw_init(psmouse);
2497 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 goto init_fail;
2499
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002500 /*
2501 * Undo part of setup done for us by psmouse core since touchpad
2502 * is not a relative device.
2503 */
2504 __clear_bit(EV_REL, dev1->evbit);
2505 __clear_bit(REL_X, dev1->relbit);
2506 __clear_bit(REL_Y, dev1->relbit);
2507
2508 /*
2509 * Now set up our capabilities.
2510 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002511 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2512 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2513 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002514 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2515 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002517 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002518
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002519 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002520 /* No pressure on V7 */
2521 if (priv->proto_version != ALPS_PROTO_V7)
2522 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002524 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002525 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2526 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002529 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002530 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2531 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002534 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002535 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2536 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2537 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2538 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002539 } else if (priv->flags & ALPS_BUTTONPAD) {
2540 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2541 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002542 } else {
2543 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2544 }
2545
Pali Rohárdfba8602015-01-09 12:48:44 -08002546 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002547 struct input_dev *dev2;
2548
2549 dev2 = input_allocate_device();
2550 if (!dev2) {
2551 psmouse_err(psmouse,
2552 "failed to allocate trackstick device\n");
2553 error = -ENOMEM;
2554 goto init_fail;
2555 }
2556
2557 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2558 psmouse->ps2dev.serio->phys);
2559 dev2->phys = priv->phys2;
2560
Pali Rohárdfba8602015-01-09 12:48:44 -08002561 /*
2562 * format of input device name is: "protocol vendor name"
2563 * see function psmouse_switch_protocol() in psmouse-base.c
2564 */
2565 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002566
2567 dev2->id.bustype = BUS_I8042;
2568 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002569 dev2->id.product = PSMOUSE_ALPS;
2570 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002571 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002572
Pali Rohár04aae282015-01-14 13:18:30 -08002573 input_set_capability(dev2, EV_REL, REL_X);
2574 input_set_capability(dev2, EV_REL, REL_Y);
2575 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2576 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2577 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002578
Pali Rohár04aae282015-01-14 13:18:30 -08002579 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002580 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2581
Pali Rohár04aae282015-01-14 13:18:30 -08002582 error = input_register_device(dev2);
2583 if (error) {
2584 psmouse_err(psmouse,
2585 "failed to register trackstick device: %d\n",
2586 error);
2587 input_free_device(dev2);
2588 goto init_fail;
2589 }
2590
2591 priv->dev2 = dev2;
2592 }
2593
2594 priv->psmouse = psmouse;
2595
2596 INIT_DELAYED_WORK(&priv->dev3_register_work,
2597 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002600 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 psmouse->disconnect = alps_disconnect;
2602 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002603 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002605 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2606 psmouse->resync_time = 0;
2607
Pali Rohár9d720b32014-11-08 12:58:57 -08002608 /* Allow 2 invalid packets without resetting device */
2609 psmouse->resetafter = psmouse->pktsize * 2;
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return 0;
2612
2613init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002614 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002615 /*
2616 * Even though we did not allocate psmouse->private we do free
2617 * it here.
2618 */
2619 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002620 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002621 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002624int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002626 struct alps_data *priv;
2627 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002629 error = alps_identify(psmouse, NULL);
2630 if (error)
2631 return error;
2632
2633 /*
2634 * Reset the device to make sure it is fully operational:
2635 * on some laptops, like certain Dell Latitudes, we may
2636 * fail to properly detect presence of trackstick if device
2637 * has not been reset.
2638 */
2639 psmouse_reset(psmouse);
2640
2641 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2642 if (!priv)
2643 return -ENOMEM;
2644
2645 error = alps_identify(psmouse, priv);
2646 if (error)
2647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 if (set_properties) {
2650 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002651 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002652 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002653 psmouse->model = priv->proto_version;
2654 } else {
2655 /*
2656 * Destroy alps_data structure we allocated earlier since
2657 * this was just a "trial run". Otherwise we'll keep it
2658 * to be used by alps_init() which has to be called if
2659 * we succeed and set_properties is true.
2660 */
2661 kfree(priv);
2662 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return 0;
2666}
2667