blob: b4144ad0987b25ef68cfe21e7b0273bcccc83f74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALPS touchpad PS/2 mouse driver
3 *
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
Peter Osterlund963f6262005-07-11 01:08:04 -05005 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08008 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
16 */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/input.h>
Seth Forshee01ce6612011-11-07 19:54:13 -080020#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/serio.h>
22#include <linux/libps2.h>
23
24#include "psmouse.h"
25#include "alps.h"
26
Seth Forshee25bded72011-11-07 19:53:36 -080027/*
28 * Definitions for ALPS version 3 and 4 command mode protocol
29 */
30#define ALPS_CMD_NIBBLE_10 0x01f2
31
Kevin Cernekeecd401202013-02-13 22:28:07 -080032#define ALPS_REG_BASE_RUSHMORE 0xc2c0
33#define ALPS_REG_BASE_PINNACLE 0x0000
34
Seth Forshee25bded72011-11-07 19:53:36 -080035static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
52};
53
54static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
71};
72
Yunkang Tang95f75e92013-12-01 22:33:52 -080073static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
90};
91
Seth Forshee25bded72011-11-07 19:53:36 -080092
Maxim Levitsky71bb21b2009-11-16 22:12:22 -080093#define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94#define ALPS_PASS 0x04 /* device has a pass-through port */
95
96#define ALPS_WHEEL 0x08 /* hardware wheel present */
97#define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98#define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99#define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800100#define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
Yunkang Tang38088432014-07-26 13:51:41 -0700102#define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Helge Dellere38de672006-09-10 21:54:39 -0400104static const struct alps_model_info alps_model_data[] = {
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
Dmitry Torokhov626b9da2015-02-15 15:55:16 -0800120
121 /*
122 * XXX This entry is suspicious. First byte has zero lower nibble,
123 * which is what a normal mouse would report. Also, the value 0x0e
124 * isn't valid per PS/2 spec.
125 */
126 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800128 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800130 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
Dmitry Torokhov8326bb52015-01-13 21:08:00 -0800131 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
134 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
135 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
137 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Dmitry Torokhov3296f712015-01-12 00:30:32 -0800140static const struct alps_protocol_info alps_v3_protocol_data = {
141 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142};
143
144static const struct alps_protocol_info alps_v3_rushmore_data = {
145 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146};
147
148static const struct alps_protocol_info alps_v5_protocol_data = {
149 ALPS_PROTO_V5, 0xc8, 0xd8, 0
150};
151
152static const struct alps_protocol_info alps_v7_protocol_data = {
153 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154};
155
Kevin Cernekee24af5cb2013-02-13 22:22:08 -0800156static void alps_set_abs_params_st(struct alps_data *priv,
157 struct input_dev *dev1);
158static void alps_set_abs_params_mt(struct alps_data *priv,
159 struct input_dev *dev1);
160
Seth Forsheed4b347b2011-11-07 19:53:15 -0800161/* Packet formats are described in Documentation/input/alps.txt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800163static bool alps_is_valid_first_byte(struct alps_data *priv,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800164 unsigned char data)
165{
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800166 return (data & priv->mask0) == priv->byte0;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800167}
168
Pali Rohár04aae282015-01-14 13:18:30 -0800169static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800170 int left, int right, int middle)
171{
Martin Buckc91ed052010-03-13 22:23:58 -0800172 struct input_dev *dev;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800173
Martin Buckc91ed052010-03-13 22:23:58 -0800174 /*
175 * If shared button has already been reported on the
176 * other device (dev2) then this event should be also
177 * sent through that device.
178 */
Pali Rohár04aae282015-01-14 13:18:30 -0800179 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800180 input_report_key(dev, BTN_LEFT, left);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800181
Pali Rohár04aae282015-01-14 13:18:30 -0800182 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800183 input_report_key(dev, BTN_RIGHT, right);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800184
Pali Rohár04aae282015-01-14 13:18:30 -0800185 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
Martin Buckc91ed052010-03-13 22:23:58 -0800186 input_report_key(dev, BTN_MIDDLE, middle);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800187
Martin Buckc91ed052010-03-13 22:23:58 -0800188 /*
189 * Sync the _other_ device now, we'll do the first
190 * device later once we report the rest of the events.
191 */
Pali Rohár04aae282015-01-14 13:18:30 -0800192 if (dev2)
193 input_sync(dev2);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -0800194}
195
Seth Forshee25bded72011-11-07 19:53:36 -0800196static void alps_process_packet_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct alps_data *priv = psmouse->private;
199 unsigned char *packet = psmouse->packet;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500200 struct input_dev *dev = psmouse->dev;
201 struct input_dev *dev2 = priv->dev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int x, y, z, ges, fin, left, right, middle;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500203 int back = 0, forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800205 if (priv->proto_version == ALPS_PROTO_V1) {
Yotam Medinid2f40122006-05-29 23:30:36 -0400206 left = packet[2] & 0x10;
207 right = packet[2] & 0x08;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 middle = 0;
209 x = packet[1] | ((packet[0] & 0x07) << 7);
210 y = packet[4] | ((packet[3] & 0x07) << 7);
211 z = packet[5];
212 } else {
213 left = packet[3] & 1;
214 right = packet[3] & 2;
215 middle = packet[3] & 4;
216 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
217 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
218 z = packet[5];
219 }
220
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800221 if (priv->flags & ALPS_FW_BK_1) {
Laszlo Kajan3c00bb92008-03-18 00:39:55 -0400222 back = packet[0] & 0x10;
223 forward = packet[2] & 4;
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500224 }
225
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800226 if (priv->flags & ALPS_FW_BK_2) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500227 back = packet[3] & 4;
228 forward = packet[2] & 4;
229 if ((middle = forward && back))
230 forward = back = 0;
231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ges = packet[2] & 1;
234 fin = packet[2] & 2;
235
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800236 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
238 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700239
Pali Rohár04aae282015-01-14 13:18:30 -0800240 alps_report_buttons(dev2, dev, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 input_sync(dev2);
243 return;
244 }
245
Pali Rohár04aae282015-01-14 13:18:30 -0800246 alps_report_buttons(dev, dev2, left, right, middle);
Ulrich Dangeld7ed5d82009-06-11 00:15:09 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Convert hardware tap to a reasonable Z value */
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800249 if (ges && !fin)
250 z = 40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /*
253 * A "tap and drag" operation is reported by the hardware as a transition
254 * from (!fin && ges) to (fin && ges). This should be translated to the
255 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
256 */
257 if (ges && fin && !priv->prev_fin) {
258 input_report_abs(dev, ABS_X, x);
259 input_report_abs(dev, ABS_Y, y);
260 input_report_abs(dev, ABS_PRESSURE, 0);
261 input_report_key(dev, BTN_TOOL_FINGER, 0);
262 input_sync(dev);
263 }
264 priv->prev_fin = fin;
265
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800266 if (z > 30)
267 input_report_key(dev, BTN_TOUCH, 1);
268 if (z < 25)
269 input_report_key(dev, BTN_TOUCH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (z > 0) {
272 input_report_abs(dev, ABS_X, x);
273 input_report_abs(dev, ABS_Y, y);
274 }
275
276 input_report_abs(dev, ABS_PRESSURE, z);
277 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
278
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800279 if (priv->flags & ALPS_WHEEL)
Vojtech Pavlike6c047b2005-09-04 01:40:43 -0500280 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800282 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Ivan Casado Ruizc30b4c12005-06-01 02:39:18 -0500283 input_report_key(dev, BTN_FORWARD, forward);
284 input_report_key(dev, BTN_BACK, back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Kevin Cernekee99df65e2013-02-13 20:56:33 -0800287 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -0800288 input_report_key(dev, BTN_0, packet[2] & 4);
289 input_report_key(dev, BTN_1, packet[0] & 0x10);
290 input_report_key(dev, BTN_2, packet[3] & 4);
291 input_report_key(dev, BTN_3, packet[0] & 0x20);
292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 input_sync(dev);
295}
296
Seth Forshee01ce6612011-11-07 19:54:13 -0800297/*
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800298 * Process bitmap data for V5 protocols. Return value is null.
299 *
300 * The bitmaps don't have enough data to track fingers, so this function
301 * only generates points representing a bounding box of at most two contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700302 * These two points are returned in fields->mt.
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800303 */
304static void alps_process_bitmap_dolphin(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700305 struct alps_fields *fields)
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800306{
307 int box_middle_x, box_middle_y;
308 unsigned int x_map, y_map;
309 unsigned char start_bit, end_bit;
310 unsigned char x_msb, x_lsb, y_msb, y_lsb;
311
312 x_map = fields->x_map;
313 y_map = fields->y_map;
314
315 if (!x_map || !y_map)
316 return;
317
318 /* Get Most-significant and Least-significant bit */
319 x_msb = fls(x_map);
320 x_lsb = ffs(x_map);
321 y_msb = fls(y_map);
322 y_lsb = ffs(y_map);
323
324 /* Most-significant bit should never exceed max sensor line number */
325 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
326 return;
327
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800328 if (fields->fingers > 1) {
329 start_bit = priv->x_bits - x_msb;
330 end_bit = priv->x_bits - x_lsb;
331 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
332 (2 * (priv->x_bits - 1));
333
334 start_bit = y_lsb - 1;
335 end_bit = y_msb - 1;
336 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
337 (2 * (priv->y_bits - 1));
Hans de Goede02d04252014-07-25 22:43:35 -0700338 fields->mt[0] = fields->st;
339 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
340 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800341 }
342}
343
Hans de Goede036e6c72014-07-25 22:38:51 -0700344static void alps_get_bitmap_points(unsigned int map,
345 struct alps_bitmap_point *low,
346 struct alps_bitmap_point *high,
347 int *fingers)
348{
349 struct alps_bitmap_point *point;
350 int i, bit, prev_bit = 0;
351
352 point = low;
353 for (i = 0; map != 0; i++, map >>= 1) {
354 bit = map & 1;
355 if (bit) {
356 if (!prev_bit) {
357 point->start_bit = i;
Hans de Goede105affb2014-07-25 22:41:51 -0700358 point->num_bits = 0;
Hans de Goede036e6c72014-07-25 22:38:51 -0700359 (*fingers)++;
360 }
361 point->num_bits++;
362 } else {
363 if (prev_bit)
364 point = high;
Hans de Goede036e6c72014-07-25 22:38:51 -0700365 }
366 prev_bit = bit;
367 }
368}
369
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800370/*
Seth Forshee01ce6612011-11-07 19:54:13 -0800371 * Process bitmap data from v3 and v4 protocols. Returns the number of
372 * fingers detected. A return value of 0 means at least one of the
373 * bitmaps was empty.
374 *
375 * The bitmaps don't have enough data to track fingers, so this function
376 * only generates points representing a bounding box of all contacts.
Hans de Goede02d04252014-07-25 22:43:35 -0700377 * These points are returned in fields->mt when the return value
Seth Forshee01ce6612011-11-07 19:54:13 -0800378 * is greater than 0.
379 */
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -0800380static int alps_process_bitmap(struct alps_data *priv,
Hans de Goede02d04252014-07-25 22:43:35 -0700381 struct alps_fields *fields)
Seth Forshee01ce6612011-11-07 19:54:13 -0800382{
Hans de Goede036e6c72014-07-25 22:38:51 -0700383 int i, fingers_x = 0, fingers_y = 0, fingers;
Seth Forshee01ce6612011-11-07 19:54:13 -0800384 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
385 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
Seth Forshee01ce6612011-11-07 19:54:13 -0800386
Hans de Goede02d04252014-07-25 22:43:35 -0700387 if (!fields->x_map || !fields->y_map)
Seth Forshee01ce6612011-11-07 19:54:13 -0800388 return 0;
389
Hans de Goede02d04252014-07-25 22:43:35 -0700390 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
391 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800392
393 /*
394 * Fingers can overlap, so we use the maximum count of fingers
395 * on either axis as the finger count.
396 */
397 fingers = max(fingers_x, fingers_y);
398
399 /*
Hans de Goede20bea682014-07-25 22:33:33 -0700400 * If an axis reports only a single contact, we have overlapping or
401 * adjacent fingers. Divide the single contact between the two points.
Seth Forshee01ce6612011-11-07 19:54:13 -0800402 */
Hans de Goede20bea682014-07-25 22:33:33 -0700403 if (fingers_x == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700404 i = (x_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700405 x_low.num_bits = x_low.num_bits - i;
406 x_high.start_bit = x_low.start_bit + i;
407 x_high.num_bits = max(i, 1);
408 }
409 if (fingers_y == 1) {
Hans de Goede28835f42014-07-25 22:42:53 -0700410 i = (y_low.num_bits - 1) / 2;
Hans de Goede20bea682014-07-25 22:33:33 -0700411 y_low.num_bits = y_low.num_bits - i;
412 y_high.start_bit = y_low.start_bit + i;
413 y_high.num_bits = max(i, 1);
Seth Forshee01ce6612011-11-07 19:54:13 -0800414 }
415
Hans de Goede02d04252014-07-25 22:43:35 -0700416 fields->mt[0].x =
417 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
418 (2 * (priv->x_bits - 1));
419 fields->mt[0].y =
420 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
421 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800422
Hans de Goede02d04252014-07-25 22:43:35 -0700423 fields->mt[1].x =
424 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
425 (2 * (priv->x_bits - 1));
426 fields->mt[1].y =
427 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
428 (2 * (priv->y_bits - 1));
Seth Forshee01ce6612011-11-07 19:54:13 -0800429
Hans de Goede40e8f532014-07-25 22:37:15 -0700430 /* y-bitmap order is reversed, except on rushmore */
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800431 if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700432 fields->mt[0].y = priv->y_max - fields->mt[0].y;
433 fields->mt[1].y = priv->y_max - fields->mt[1].y;
Hans de Goede40e8f532014-07-25 22:37:15 -0700434 }
435
Seth Forshee01ce6612011-11-07 19:54:13 -0800436 return fingers;
437}
438
Hans de Goedecdf333e2014-07-25 22:44:42 -0700439static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
Seth Forshee01ce6612011-11-07 19:54:13 -0800440{
441 input_mt_slot(dev, slot);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700442 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
443 input_report_abs(dev, ABS_MT_POSITION_X, x);
444 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Seth Forshee01ce6612011-11-07 19:54:13 -0800445}
446
Hans de Goedecdf333e2014-07-25 22:44:42 -0700447static void alps_report_mt_data(struct psmouse *psmouse, int n)
Seth Forshee01ce6612011-11-07 19:54:13 -0800448{
Hans de Goede02d04252014-07-25 22:43:35 -0700449 struct alps_data *priv = psmouse->private;
450 struct input_dev *dev = psmouse->dev;
451 struct alps_fields *f = &priv->f;
Hans de Goedecdf333e2014-07-25 22:44:42 -0700452 int i, slot[MAX_TOUCHES];
Hans de Goede02d04252014-07-25 22:43:35 -0700453
Henrik Rydberg448c7f382015-02-01 11:25:14 -0800454 input_mt_assign_slots(dev, slot, f->mt, n, 0);
Hans de Goedecdf333e2014-07-25 22:44:42 -0700455 for (i = 0; i < n; i++)
456 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
457
458 input_mt_sync_frame(dev);
Seth Forshee01ce6612011-11-07 19:54:13 -0800459}
460
Hans de Goede68c21872014-07-25 22:47:25 -0700461static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
462{
463 struct alps_data *priv = psmouse->private;
464 struct input_dev *dev = psmouse->dev;
465 struct alps_fields *f = &priv->f;
466
467 /* Use st data when we don't have mt data */
468 if (fingers < 2) {
469 f->mt[0].x = f->st.x;
470 f->mt[0].y = f->st.y;
471 fingers = f->pressure > 0 ? 1 : 0;
472 }
473
Hans de Goede99d99962014-07-25 22:48:02 -0700474 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
Hans de Goede68c21872014-07-25 22:47:25 -0700475
476 input_mt_report_finger_count(dev, fingers);
477
478 input_report_key(dev, BTN_LEFT, f->left);
479 input_report_key(dev, BTN_RIGHT, f->right);
480 input_report_key(dev, BTN_MIDDLE, f->middle);
481
482 input_report_abs(dev, ABS_PRESSURE, f->pressure);
483
484 input_sync(dev);
485}
486
Seth Forshee25bded72011-11-07 19:53:36 -0800487static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
488{
489 struct alps_data *priv = psmouse->private;
490 unsigned char *packet = psmouse->packet;
491 struct input_dev *dev = priv->dev2;
492 int x, y, z, left, right, middle;
493
Pali Rohár34412ba2015-01-09 12:48:58 -0800494 /* It should be a DualPoint when received trackstick packet */
495 if (!(priv->flags & ALPS_DUALPOINT)) {
496 psmouse_warn(psmouse,
497 "Rejected trackstick packet from non DualPoint device");
498 return;
499 }
500
Seth Forshee25bded72011-11-07 19:53:36 -0800501 /* Sanity check packet */
502 if (!(packet[0] & 0x40)) {
503 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
504 return;
505 }
506
507 /*
508 * There's a special packet that seems to indicate the end
509 * of a stream of trackstick data. Filter these out.
510 */
511 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
512 return;
513
514 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
515 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
516 z = (packet[4] & 0x7c) >> 2;
517
518 /*
519 * The x and y values tend to be quite large, and when used
520 * alone the trackstick is difficult to use. Scale them down
521 * to compensate.
522 */
523 x /= 8;
524 y /= 8;
525
526 input_report_rel(dev, REL_X, x);
527 input_report_rel(dev, REL_Y, -y);
528
529 /*
530 * Most ALPS models report the trackstick buttons in the touchpad
531 * packets, but a few report them here. No reliable way has been
532 * found to differentiate between the models upfront, so we enable
533 * the quirk in response to seeing a button press in the trackstick
534 * packet.
535 */
536 left = packet[3] & 0x01;
537 right = packet[3] & 0x02;
538 middle = packet[3] & 0x04;
539
540 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
541 (left || right || middle))
542 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
543
544 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
545 input_report_key(dev, BTN_LEFT, left);
546 input_report_key(dev, BTN_RIGHT, right);
547 input_report_key(dev, BTN_MIDDLE, middle);
548 }
549
550 input_sync(dev);
551 return;
552}
553
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800554static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
555{
556 f->left = !!(p[3] & 0x01);
557 f->right = !!(p[3] & 0x02);
558 f->middle = !!(p[3] & 0x04);
559
560 f->ts_left = !!(p[3] & 0x10);
561 f->ts_right = !!(p[3] & 0x20);
562 f->ts_middle = !!(p[3] & 0x40);
563}
564
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700565static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800566 struct psmouse *psmouse)
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800567{
568 f->first_mp = !!(p[4] & 0x40);
569 f->is_mp = !!(p[0] & 0x40);
570
571 f->fingers = (p[5] & 0x3) + 1;
572 f->x_map = ((p[4] & 0x7e) << 8) |
573 ((p[1] & 0x7f) << 2) |
574 ((p[0] & 0x30) >> 4);
575 f->y_map = ((p[3] & 0x70) << 4) |
576 ((p[2] & 0x7f) << 1) |
577 (p[4] & 0x01);
578
Hans de Goede02d04252014-07-25 22:43:35 -0700579 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800580 ((p[0] & 0x30) >> 4);
Hans de Goede02d04252014-07-25 22:43:35 -0700581 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
582 f->pressure = p[5] & 0x7f;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800583
584 alps_decode_buttons_v3(f, p);
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700585
586 return 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800587}
588
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700589static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800590 struct psmouse *psmouse)
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800591{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800592 alps_decode_pinnacle(f, p, psmouse);
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800593
Yunkang Tangf105e342014-07-25 22:29:24 -0700594 /* Rushmore's packet decode has a bit difference with Pinnacle's */
595 f->is_mp = !!(p[5] & 0x40);
596 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800597 f->x_map |= (p[5] & 0x10) << 11;
598 f->y_map |= (p[5] & 0x20) << 6;
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700599
600 return 0;
Kevin Cernekee1302bac2013-02-13 22:27:08 -0800601}
602
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700603static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800604 struct psmouse *psmouse)
Dave Turvene75af9e52013-02-21 22:58:28 -0800605{
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800606 u64 palm_data = 0;
607 struct alps_data *priv = psmouse->private;
608
Dave Turvene75af9e52013-02-21 22:58:28 -0800609 f->first_mp = !!(p[0] & 0x02);
610 f->is_mp = !!(p[0] & 0x20);
611
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800612 if (!f->is_mp) {
Hans de Goede02d04252014-07-25 22:43:35 -0700613 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
614 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
615 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800616 alps_decode_buttons_v3(f, p);
617 } else {
618 f->fingers = ((p[0] & 0x6) >> 1 |
Dave Turvene75af9e52013-02-21 22:58:28 -0800619 (p[0] & 0x10) >> 2);
Dave Turvene75af9e52013-02-21 22:58:28 -0800620
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800621 palm_data = (p[1] & 0x7f) |
622 ((p[2] & 0x7f) << 7) |
623 ((p[4] & 0x7f) << 14) |
624 ((p[5] & 0x7f) << 21) |
625 ((p[3] & 0x07) << 28) |
626 (((u64)p[3] & 0x70) << 27) |
627 (((u64)p[0] & 0x01) << 34);
Dave Turvene75af9e52013-02-21 22:58:28 -0800628
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800629 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
630 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
631
632 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
633 f->x_map = (palm_data >> priv->y_bits) &
634 (BIT(priv->x_bits) - 1);
635 }
Hans de Goede38c11eaa2014-07-25 22:48:44 -0700636
637 return 0;
Dave Turvene75af9e52013-02-21 22:58:28 -0800638}
639
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800640static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -0800641{
642 struct alps_data *priv = psmouse->private;
643 unsigned char *packet = psmouse->packet;
Seth Forshee25bded72011-11-07 19:53:36 -0800644 struct input_dev *dev2 = priv->dev2;
Hans de Goede02d04252014-07-25 22:43:35 -0700645 struct alps_fields *f = &priv->f;
646 int fingers = 0;
Kevin Cernekeef85e5002013-02-13 22:26:11 -0800647
Hans de Goede02d04252014-07-25 22:43:35 -0700648 memset(f, 0, sizeof(*f));
649
650 priv->decode_fields(f, packet, psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800651
652 /*
Seth Forshee01ce6612011-11-07 19:54:13 -0800653 * There's no single feature of touchpad position and bitmap packets
654 * that can be used to distinguish between them. We rely on the fact
655 * that a bitmap packet should always follow a position packet with
656 * bit 6 of packet[4] set.
Seth Forshee25bded72011-11-07 19:53:36 -0800657 */
658 if (priv->multi_packet) {
Seth Forshee25bded72011-11-07 19:53:36 -0800659 /*
660 * Sometimes a position packet will indicate a multi-packet
661 * sequence, but then what follows is another position
662 * packet. Check for this, and when it happens process the
663 * position packet as usual.
664 */
Hans de Goede02d04252014-07-25 22:43:35 -0700665 if (f->is_mp) {
666 fingers = f->fingers;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -0800667 if (priv->proto_version == ALPS_PROTO_V3 ||
668 priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
Hans de Goede02d04252014-07-25 22:43:35 -0700669 if (alps_process_bitmap(priv, f) == 0)
Hans de Goede20bea682014-07-25 22:33:33 -0700670 fingers = 0; /* Use st data */
Seth Forshee01ce6612011-11-07 19:54:13 -0800671
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800672 /* Now process position packet */
Hans de Goede02d04252014-07-25 22:43:35 -0700673 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800674 psmouse);
675 } else {
676 /*
677 * Because Dolphin uses position packet's
678 * coordinate data as Pt1 and uses it to
679 * calculate Pt2, so we need to do position
680 * packet decode first.
681 */
Hans de Goede02d04252014-07-25 22:43:35 -0700682 priv->decode_fields(f, priv->multi_data,
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800683 psmouse);
684
685 /*
686 * Since Dolphin's finger number is reliable,
687 * there is no need to compare with bmap_fn.
688 */
Hans de Goede02d04252014-07-25 22:43:35 -0700689 alps_process_bitmap_dolphin(priv, f);
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800690 }
Seth Forshee01ce6612011-11-07 19:54:13 -0800691 } else {
692 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800693 }
694 }
695
Seth Forshee01ce6612011-11-07 19:54:13 -0800696 /*
697 * Bit 6 of byte 0 is not usually set in position packets. The only
698 * times it seems to be set is in situations where the data is
699 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
700 * this combined with the fact that this bit is useful for filtering
701 * out misidentified bitmap packets, we reject anything with this
702 * bit set.
703 */
Hans de Goede02d04252014-07-25 22:43:35 -0700704 if (f->is_mp)
Seth Forshee01ce6612011-11-07 19:54:13 -0800705 return;
706
Hans de Goede02d04252014-07-25 22:43:35 -0700707 if (!priv->multi_packet && f->first_mp) {
Seth Forshee25bded72011-11-07 19:53:36 -0800708 priv->multi_packet = 1;
Seth Forshee01ce6612011-11-07 19:54:13 -0800709 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
710 return;
711 }
712
713 priv->multi_packet = 0;
Seth Forshee25bded72011-11-07 19:53:36 -0800714
Seth Forshee25bded72011-11-07 19:53:36 -0800715 /*
716 * Sometimes the hardware sends a single packet with z = 0
717 * in the middle of a stream. Real releases generate packets
718 * with x, y, and z all zero, so these seem to be flukes.
719 * Ignore them.
720 */
Hans de Goede02d04252014-07-25 22:43:35 -0700721 if (f->st.x && f->st.y && !f->pressure)
Seth Forshee25bded72011-11-07 19:53:36 -0800722 return;
723
Hans de Goede68c21872014-07-25 22:47:25 -0700724 alps_report_semi_mt_data(psmouse, fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800725
Pali Rohár34412ba2015-01-09 12:48:58 -0800726 if ((priv->flags & ALPS_DUALPOINT) &&
727 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
Hans de Goede02d04252014-07-25 22:43:35 -0700728 input_report_key(dev2, BTN_LEFT, f->ts_left);
729 input_report_key(dev2, BTN_RIGHT, f->ts_right);
730 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
Seth Forshee25bded72011-11-07 19:53:36 -0800731 input_sync(dev2);
732 }
733}
734
735static void alps_process_packet_v3(struct psmouse *psmouse)
736{
737 unsigned char *packet = psmouse->packet;
738
739 /*
740 * v3 protocol packets come in three types, two representing
741 * touchpad data and one representing trackstick data.
742 * Trackstick packets seem to be distinguished by always
743 * having 0x3f in the last byte. This value has never been
744 * observed in the last byte of either of the other types
745 * of packets.
746 */
747 if (packet[5] == 0x3f) {
748 alps_process_trackstick_packet_v3(psmouse);
749 return;
750 }
751
Yunkang Tangee65d4b2013-12-26 14:54:19 -0800752 alps_process_touchpad_packet_v3_v5(psmouse);
Seth Forshee25bded72011-11-07 19:53:36 -0800753}
754
Yunkang Tang95f75e92013-12-01 22:33:52 -0800755static void alps_process_packet_v6(struct psmouse *psmouse)
756{
757 struct alps_data *priv = psmouse->private;
758 unsigned char *packet = psmouse->packet;
759 struct input_dev *dev = psmouse->dev;
760 struct input_dev *dev2 = priv->dev2;
761 int x, y, z, left, right, middle;
762
763 /*
764 * We can use Byte5 to distinguish if the packet is from Touchpad
765 * or Trackpoint.
766 * Touchpad: 0 - 0x7E
767 * Trackpoint: 0x7F
768 */
769 if (packet[5] == 0x7F) {
770 /* It should be a DualPoint when received Trackpoint packet */
Pali Rohár34412ba2015-01-09 12:48:58 -0800771 if (!(priv->flags & ALPS_DUALPOINT)) {
772 psmouse_warn(psmouse,
773 "Rejected trackstick packet from non DualPoint device");
Yunkang Tang95f75e92013-12-01 22:33:52 -0800774 return;
Pali Rohár34412ba2015-01-09 12:48:58 -0800775 }
Yunkang Tang95f75e92013-12-01 22:33:52 -0800776
777 /* Trackpoint packet */
778 x = packet[1] | ((packet[3] & 0x20) << 2);
779 y = packet[2] | ((packet[3] & 0x40) << 1);
780 z = packet[4];
781 left = packet[3] & 0x01;
782 right = packet[3] & 0x02;
783 middle = packet[3] & 0x04;
784
785 /* To prevent the cursor jump when finger lifted */
786 if (x == 0x7F && y == 0x7F && z == 0x7F)
787 x = y = z = 0;
788
789 /* Divide 4 since trackpoint's speed is too fast */
790 input_report_rel(dev2, REL_X, (char)x / 4);
791 input_report_rel(dev2, REL_Y, -((char)y / 4));
792
793 input_report_key(dev2, BTN_LEFT, left);
794 input_report_key(dev2, BTN_RIGHT, right);
795 input_report_key(dev2, BTN_MIDDLE, middle);
796
797 input_sync(dev2);
798 return;
799 }
800
801 /* Touchpad packet */
802 x = packet[1] | ((packet[3] & 0x78) << 4);
803 y = packet[2] | ((packet[4] & 0x78) << 4);
804 z = packet[5];
805 left = packet[3] & 0x01;
806 right = packet[3] & 0x02;
807
808 if (z > 30)
809 input_report_key(dev, BTN_TOUCH, 1);
810 if (z < 25)
811 input_report_key(dev, BTN_TOUCH, 0);
812
813 if (z > 0) {
814 input_report_abs(dev, ABS_X, x);
815 input_report_abs(dev, ABS_Y, y);
816 }
817
818 input_report_abs(dev, ABS_PRESSURE, z);
819 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
820
821 /* v6 touchpad does not have middle button */
822 input_report_key(dev, BTN_LEFT, left);
823 input_report_key(dev, BTN_RIGHT, right);
824
825 input_sync(dev);
826}
827
Seth Forshee25bded72011-11-07 19:53:36 -0800828static void alps_process_packet_v4(struct psmouse *psmouse)
829{
George Pantalos3b7e09f2012-05-10 22:31:59 -0700830 struct alps_data *priv = psmouse->private;
Seth Forshee25bded72011-11-07 19:53:36 -0800831 unsigned char *packet = psmouse->packet;
Hans de Goede02d04252014-07-25 22:43:35 -0700832 struct alps_fields *f = &priv->f;
Hans de Goede68c21872014-07-25 22:47:25 -0700833 int offset;
George Pantalos3b7e09f2012-05-10 22:31:59 -0700834
835 /*
836 * v4 has a 6-byte encoding for bitmap data, but this data is
837 * broken up between 3 normal packets. Use priv->multi_packet to
838 * track our position in the bitmap packet.
839 */
840 if (packet[6] & 0x40) {
841 /* sync, reset position */
842 priv->multi_packet = 0;
843 }
844
845 if (WARN_ON_ONCE(priv->multi_packet > 2))
846 return;
847
848 offset = 2 * priv->multi_packet;
849 priv->multi_data[offset] = packet[6];
850 priv->multi_data[offset + 1] = packet[7];
851
852 if (++priv->multi_packet > 2) {
853 priv->multi_packet = 0;
854
Hans de Goede02d04252014-07-25 22:43:35 -0700855 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700856 ((priv->multi_data[3] & 0x60) << 3) |
857 ((priv->multi_data[0] & 0x3f) << 2) |
858 ((priv->multi_data[1] & 0x60) >> 5);
Hans de Goede02d04252014-07-25 22:43:35 -0700859 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
George Pantalos3b7e09f2012-05-10 22:31:59 -0700860 ((priv->multi_data[3] & 0x1f) << 5) |
861 (priv->multi_data[1] & 0x1f);
862
Hans de Goede02d04252014-07-25 22:43:35 -0700863 f->fingers = alps_process_bitmap(priv, f);
George Pantalos3b7e09f2012-05-10 22:31:59 -0700864 }
Seth Forshee25bded72011-11-07 19:53:36 -0800865
Andreas Boschb0cfb792014-10-15 10:44:50 -0700866 f->left = !!(packet[4] & 0x01);
867 f->right = !!(packet[4] & 0x02);
Seth Forshee25bded72011-11-07 19:53:36 -0800868
Hans de Goede02d04252014-07-25 22:43:35 -0700869 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
870 ((packet[0] & 0x30) >> 4);
871 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
872 f->pressure = packet[5] & 0x7f;
Seth Forshee25bded72011-11-07 19:53:36 -0800873
Hans de Goede68c21872014-07-25 22:47:25 -0700874 alps_report_semi_mt_data(psmouse, f->fingers);
Seth Forshee25bded72011-11-07 19:53:36 -0800875}
876
Yunkang Tang38088432014-07-26 13:51:41 -0700877static bool alps_is_valid_package_v7(struct psmouse *psmouse)
878{
879 switch (psmouse->pktcnt) {
880 case 3:
881 return (psmouse->packet[2] & 0x40) == 0x40;
882 case 4:
883 return (psmouse->packet[3] & 0x48) == 0x48;
884 case 6:
885 return (psmouse->packet[5] & 0x40) == 0x00;
886 }
887 return true;
888}
889
890static unsigned char alps_get_packet_id_v7(char *byte)
891{
892 unsigned char packet_id;
893
894 if (byte[4] & 0x40)
895 packet_id = V7_PACKET_ID_TWO;
896 else if (byte[4] & 0x01)
897 packet_id = V7_PACKET_ID_MULTI;
898 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
899 packet_id = V7_PACKET_ID_NEW;
900 else if (byte[1] == 0x00 && byte[4] == 0x00)
901 packet_id = V7_PACKET_ID_IDLE;
902 else
903 packet_id = V7_PACKET_ID_UNKNOWN;
904
905 return packet_id;
906}
907
908static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
909 unsigned char *pkt,
910 unsigned char pkt_id)
911{
Hans de Goede27a560b2014-12-18 09:54:50 -0800912 /*
913 * packet-fmt b7 b6 b5 b4 b3 b2 b1 b0
914 * Byte0 TWO & MULTI L 1 R M 1 Y0-2 Y0-1 Y0-0
915 * Byte0 NEW L 1 X1-5 1 1 Y0-2 Y0-1 Y0-0
916 * Byte1 Y0-10 Y0-9 Y0-8 Y0-7 Y0-6 Y0-5 Y0-4 Y0-3
917 * Byte2 X0-11 1 X0-10 X0-9 X0-8 X0-7 X0-6 X0-5
918 * Byte3 X1-11 1 X0-4 X0-3 1 X0-2 X0-1 X0-0
919 * Byte4 TWO X1-10 TWO X1-9 X1-8 X1-7 X1-6 X1-5 X1-4
920 * Byte4 MULTI X1-10 TWO X1-9 X1-8 X1-7 X1-6 Y1-5 1
921 * Byte4 NEW X1-10 TWO X1-9 X1-8 X1-7 X1-6 0 0
922 * Byte5 TWO & NEW Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 Y1-5 Y1-4
923 * Byte5 MULTI Y1-10 0 Y1-9 Y1-8 Y1-7 Y1-6 F-1 F-0
924 * L: Left button
925 * R / M: Non-clickpads: Right / Middle button
926 * Clickpads: When > 2 fingers are down, and some fingers
927 * are in the button area, then the 2 coordinates reported
928 * are for fingers outside the button area and these report
929 * extra fingers being present in the right / left button
930 * area. Note these fingers are not added to the F field!
931 * so if a TWO packet is received and R = 1 then there are
932 * 3 fingers down, etc.
933 * TWO: 1: Two touches present, byte 0/4/5 are in TWO fmt
934 * 0: If byte 4 bit 0 is 1, then byte 0/4/5 are in MULTI fmt
935 * otherwise byte 0 bit 4 must be set and byte 0/4/5 are
936 * in NEW fmt
937 * F: Number of fingers - 3, 0 means 3 fingers, 1 means 4 ...
938 */
939
Yunkang Tang38088432014-07-26 13:51:41 -0700940 mt[0].x = ((pkt[2] & 0x80) << 4);
941 mt[0].x |= ((pkt[2] & 0x3F) << 5);
942 mt[0].x |= ((pkt[3] & 0x30) >> 1);
943 mt[0].x |= (pkt[3] & 0x07);
944 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
945
946 mt[1].x = ((pkt[3] & 0x80) << 4);
947 mt[1].x |= ((pkt[4] & 0x80) << 3);
948 mt[1].x |= ((pkt[4] & 0x3F) << 4);
949 mt[1].y = ((pkt[5] & 0x80) << 3);
950 mt[1].y |= ((pkt[5] & 0x3F) << 4);
951
952 switch (pkt_id) {
953 case V7_PACKET_ID_TWO:
954 mt[1].x &= ~0x000F;
955 mt[1].y |= 0x000F;
956 break;
957
958 case V7_PACKET_ID_MULTI:
959 mt[1].x &= ~0x003F;
960 mt[1].y &= ~0x0020;
961 mt[1].y |= ((pkt[4] & 0x02) << 4);
962 mt[1].y |= 0x001F;
963 break;
964
965 case V7_PACKET_ID_NEW:
966 mt[1].x &= ~0x003F;
967 mt[1].x |= (pkt[0] & 0x20);
968 mt[1].y |= 0x000F;
969 break;
970 }
971
972 mt[0].y = 0x7FF - mt[0].y;
973 mt[1].y = 0x7FF - mt[1].y;
974}
975
976static int alps_get_mt_count(struct input_mt_pos *mt)
977{
Hans de Goede7091c442014-12-18 09:53:34 -0800978 int i, fingers = 0;
Yunkang Tang38088432014-07-26 13:51:41 -0700979
Hans de Goede7091c442014-12-18 09:53:34 -0800980 for (i = 0; i < MAX_TOUCHES; i++) {
981 if (mt[i].x != 0 || mt[i].y != 0)
982 fingers++;
983 }
Yunkang Tang38088432014-07-26 13:51:41 -0700984
Hans de Goede7091c442014-12-18 09:53:34 -0800985 return fingers;
Yunkang Tang38088432014-07-26 13:51:41 -0700986}
987
988static int alps_decode_packet_v7(struct alps_fields *f,
989 unsigned char *p,
990 struct psmouse *psmouse)
991{
Hans de Goeded27eb792014-12-18 09:55:14 -0800992 struct alps_data *priv = psmouse->private;
Yunkang Tang38088432014-07-26 13:51:41 -0700993 unsigned char pkt_id;
994
995 pkt_id = alps_get_packet_id_v7(p);
996 if (pkt_id == V7_PACKET_ID_IDLE)
997 return 0;
998 if (pkt_id == V7_PACKET_ID_UNKNOWN)
999 return -1;
Hans de Goede8b238112014-12-18 09:52:59 -08001000 /*
1001 * NEW packets are send to indicate a discontinuity in the finger
1002 * coordinate reporting. Specifically a finger may have moved from
1003 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1004 * us.
1005 *
1006 * NEW packets have 3 problems:
1007 * 1) They do not contain middle / right button info (on non clickpads)
1008 * this can be worked around by preserving the old button state
1009 * 2) They do not contain an accurate fingercount, and they are
1010 * typically send when the number of fingers changes. We cannot use
1011 * the old finger count as that may mismatch with the amount of
1012 * touch coordinates we've available in the NEW packet
1013 * 3) Their x data for the second touch is inaccurate leading to
1014 * a possible jump of the x coordinate by 16 units when the first
1015 * non NEW packet comes in
1016 * Since problems 2 & 3 cannot be worked around, just ignore them.
1017 */
1018 if (pkt_id == V7_PACKET_ID_NEW)
1019 return 1;
Yunkang Tang38088432014-07-26 13:51:41 -07001020
1021 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1022
Yunkang Tang38088432014-07-26 13:51:41 -07001023 if (pkt_id == V7_PACKET_ID_TWO)
1024 f->fingers = alps_get_mt_count(f->mt);
Hans de Goede8b238112014-12-18 09:52:59 -08001025 else /* pkt_id == V7_PACKET_ID_MULTI */
Yunkang Tang38088432014-07-26 13:51:41 -07001026 f->fingers = 3 + (p[5] & 0x03);
1027
Hans de Goeded27eb792014-12-18 09:55:14 -08001028 f->left = (p[0] & 0x80) >> 7;
1029 if (priv->flags & ALPS_BUTTONPAD) {
1030 if (p[0] & 0x20)
1031 f->fingers++;
1032 if (p[0] & 0x10)
1033 f->fingers++;
1034 } else {
1035 f->right = (p[0] & 0x20) >> 5;
1036 f->middle = (p[0] & 0x10) >> 4;
1037 }
1038
Hans de Goede7091c442014-12-18 09:53:34 -08001039 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1040 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1041 f->mt[0].x = f->mt[1].x;
1042 f->mt[0].y = f->mt[1].y;
1043 f->mt[1].x = 0;
1044 f->mt[1].y = 0;
1045 }
1046
Yunkang Tang38088432014-07-26 13:51:41 -07001047 return 0;
1048}
1049
1050static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1051{
1052 struct alps_data *priv = psmouse->private;
1053 unsigned char *packet = psmouse->packet;
1054 struct input_dev *dev2 = priv->dev2;
1055 int x, y, z, left, right, middle;
1056
Pali Rohár34412ba2015-01-09 12:48:58 -08001057 /* It should be a DualPoint when received trackstick packet */
1058 if (!(priv->flags & ALPS_DUALPOINT)) {
1059 psmouse_warn(psmouse,
1060 "Rejected trackstick packet from non DualPoint device");
1061 return;
1062 }
1063
Yunkang Tang38088432014-07-26 13:51:41 -07001064 /*
1065 * b7 b6 b5 b4 b3 b2 b1 b0
1066 * Byte0 0 1 0 0 1 0 0 0
1067 * Byte1 1 1 * * 1 M R L
1068 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
1069 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
1070 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
1071 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
1072 * M / R / L: Middle / Right / Left button
1073 */
1074
1075 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1076 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1077 ((packet[3] & 0x20) << 1);
1078 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1079
1080 left = (packet[1] & 0x01);
1081 right = (packet[1] & 0x02) >> 1;
1082 middle = (packet[1] & 0x04) >> 2;
1083
1084 /* Divide 2 since trackpoint's speed is too fast */
1085 input_report_rel(dev2, REL_X, (char)x / 2);
1086 input_report_rel(dev2, REL_Y, -((char)y / 2));
1087
1088 input_report_key(dev2, BTN_LEFT, left);
1089 input_report_key(dev2, BTN_RIGHT, right);
1090 input_report_key(dev2, BTN_MIDDLE, middle);
1091
1092 input_sync(dev2);
1093}
1094
1095static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1096{
1097 struct alps_data *priv = psmouse->private;
1098 struct input_dev *dev = psmouse->dev;
1099 struct alps_fields *f = &priv->f;
1100
1101 memset(f, 0, sizeof(*f));
1102
1103 if (priv->decode_fields(f, psmouse->packet, psmouse))
1104 return;
1105
1106 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1107
1108 input_mt_report_finger_count(dev, f->fingers);
1109
1110 input_report_key(dev, BTN_LEFT, f->left);
1111 input_report_key(dev, BTN_RIGHT, f->right);
1112 input_report_key(dev, BTN_MIDDLE, f->middle);
1113
1114 input_sync(dev);
1115}
1116
1117static void alps_process_packet_v7(struct psmouse *psmouse)
1118{
1119 unsigned char *packet = psmouse->packet;
1120
1121 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1122 alps_process_trackstick_packet_v7(psmouse);
1123 else
1124 alps_process_touchpad_packet_v7(psmouse);
1125}
1126
Pali Rohár04aae282015-01-14 13:18:30 -08001127static DEFINE_MUTEX(alps_mutex);
1128
1129static void alps_register_bare_ps2_mouse(struct work_struct *work)
1130{
1131 struct alps_data *priv =
1132 container_of(work, struct alps_data, dev3_register_work.work);
1133 struct psmouse *psmouse = priv->psmouse;
1134 struct input_dev *dev3;
1135 int error = 0;
1136
1137 mutex_lock(&alps_mutex);
1138
1139 if (priv->dev3)
1140 goto out;
1141
1142 dev3 = input_allocate_device();
1143 if (!dev3) {
1144 psmouse_err(psmouse, "failed to allocate secondary device\n");
1145 error = -ENOMEM;
1146 goto out;
1147 }
1148
1149 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1150 psmouse->ps2dev.serio->phys,
1151 (priv->dev2 ? "input2" : "input1"));
1152 dev3->phys = priv->phys3;
1153
1154 /*
1155 * format of input device name is: "protocol vendor name"
1156 * see function psmouse_switch_protocol() in psmouse-base.c
1157 */
1158 dev3->name = "PS/2 ALPS Mouse";
1159
1160 dev3->id.bustype = BUS_I8042;
1161 dev3->id.vendor = 0x0002;
1162 dev3->id.product = PSMOUSE_PS2;
1163 dev3->id.version = 0x0000;
1164 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1165
1166 input_set_capability(dev3, EV_REL, REL_X);
1167 input_set_capability(dev3, EV_REL, REL_Y);
1168 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1169 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1170 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1171
1172 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1173
1174 error = input_register_device(dev3);
1175 if (error) {
1176 psmouse_err(psmouse,
1177 "failed to register secondary device: %d\n",
1178 error);
1179 input_free_device(dev3);
1180 goto out;
1181 }
1182
1183 priv->dev3 = dev3;
1184
1185out:
1186 /*
1187 * Save the error code so that we can detect that we
1188 * already tried to create the device.
1189 */
1190 if (error)
1191 priv->dev3 = ERR_PTR(error);
1192
1193 mutex_unlock(&alps_mutex);
1194}
1195
1196static void alps_report_bare_ps2_packet(struct input_dev *dev,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001197 unsigned char packet[],
1198 bool report_buttons)
1199{
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001200 if (report_buttons)
Pali Rohár04aae282015-01-14 13:18:30 -08001201 alps_report_buttons(dev, NULL,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001202 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1203
Pali Rohár04aae282015-01-14 13:18:30 -08001204 input_report_rel(dev, REL_X,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001205 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
Pali Rohár04aae282015-01-14 13:18:30 -08001206 input_report_rel(dev, REL_Y,
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001207 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1208
Pali Rohár04aae282015-01-14 13:18:30 -08001209 input_sync(dev);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001210}
1211
1212static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct alps_data *priv = psmouse->private;
1215
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001216 if (psmouse->pktcnt < 6)
1217 return PSMOUSE_GOOD_DATA;
1218
1219 if (psmouse->pktcnt == 6) {
1220 /*
1221 * Start a timer to flush the packet if it ends up last
1222 * 6-byte packet in the stream. Timer needs to fire
1223 * psmouse core times out itself. 20 ms should be enough
1224 * to decide if we are getting more data or not.
1225 */
1226 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1227 return PSMOUSE_GOOD_DATA;
1228 }
1229
1230 del_timer(&priv->timer);
1231
1232 if (psmouse->packet[6] & 0x80) {
1233
1234 /*
1235 * Highest bit is set - that means we either had
1236 * complete ALPS packet and this is start of the
1237 * next packet or we got garbage.
1238 */
1239
1240 if (((psmouse->packet[3] |
1241 psmouse->packet[4] |
1242 psmouse->packet[5]) & 0x80) ||
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001243 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001244 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001245 "refusing packet %4ph (suspected interleaved ps/2)\n",
1246 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001247 return PSMOUSE_BAD_DATA;
1248 }
1249
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001250 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001251
1252 /* Continue with the next packet */
1253 psmouse->packet[0] = psmouse->packet[6];
1254 psmouse->pktcnt = 1;
1255
1256 } else {
1257
1258 /*
1259 * High bit is 0 - that means that we indeed got a PS/2
1260 * packet in the middle of ALPS packet.
1261 *
1262 * There is also possibility that we got 6-byte ALPS
1263 * packet followed by 3-byte packet from trackpoint. We
1264 * can not distinguish between these 2 scenarios but
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001265 * because the latter is unlikely to happen in course of
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001266 * normal operation (user would need to press all
1267 * buttons on the pad and start moving trackpoint
1268 * without touching the pad surface) we assume former.
1269 * Even if we are wrong the wost thing that would happen
1270 * the cursor would jump but we should not get protocol
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001271 * de-synchronization.
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001272 */
1273
Pali Rohár04aae282015-01-14 13:18:30 -08001274 alps_report_bare_ps2_packet(priv->dev2,
1275 &psmouse->packet[3], false);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001276
1277 /*
1278 * Continue with the standard ALPS protocol handling,
1279 * but make sure we won't process it as an interleaved
1280 * packet again, which may happen if all buttons are
1281 * pressed. To avoid this let's reset the 4th bit which
1282 * is normally 1.
1283 */
1284 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1285 psmouse->pktcnt = 4;
1286 }
1287
1288 return PSMOUSE_GOOD_DATA;
1289}
1290
1291static void alps_flush_packet(unsigned long data)
1292{
1293 struct psmouse *psmouse = (struct psmouse *)data;
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001294 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001295
1296 serio_pause_rx(psmouse->ps2dev.serio);
1297
Seth Forsheeb46615f2011-11-07 19:53:30 -08001298 if (psmouse->pktcnt == psmouse->pktsize) {
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001299
1300 /*
1301 * We did not any more data in reasonable amount of time.
1302 * Validate the last 3 bytes and process as a standard
1303 * ALPS packet.
1304 */
1305 if ((psmouse->packet[3] |
1306 psmouse->packet[4] |
1307 psmouse->packet[5]) & 0x80) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001308 psmouse_dbg(psmouse,
Andy Shevchenko3b112922012-10-30 00:24:41 -07001309 "refusing packet %3ph (suspected interleaved ps/2)\n",
1310 psmouse->packet + 3);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001311 } else {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001312 priv->process_packet(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001313 }
1314 psmouse->pktcnt = 0;
1315 }
1316
1317 serio_continue_rx(psmouse->ps2dev.serio);
1318}
1319
1320static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1321{
1322 struct alps_data *priv = psmouse->private;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001323
Pali Rohár4ab8f7f2014-11-08 12:45:23 -08001324 /*
1325 * Check if we are dealing with a bare PS/2 packet, presumably from
1326 * a device connected to the external PS/2 port. Because bare PS/2
1327 * protocol does not have enough constant bits to self-synchronize
1328 * properly we only do this if the device is fully synchronized.
1329 */
1330 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
Pali Rohár04aae282015-01-14 13:18:30 -08001331
1332 /* Register dev3 mouse if we received PS/2 packet first time */
1333 if (unlikely(!priv->dev3))
1334 psmouse_queue_work(psmouse,
1335 &priv->dev3_register_work, 0);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (psmouse->pktcnt == 3) {
Pali Rohár04aae282015-01-14 13:18:30 -08001338 /* Once dev3 mouse device is registered report data */
1339 if (likely(!IS_ERR_OR_NULL(priv->dev3)))
1340 alps_report_bare_ps2_packet(priv->dev3,
1341 psmouse->packet,
1342 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return PSMOUSE_FULL_PACKET;
1344 }
1345 return PSMOUSE_GOOD_DATA;
1346 }
1347
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001348 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1349
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001350 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001351 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1352 return alps_handle_interleaved_ps2(psmouse);
1353 }
1354
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001355 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001356 psmouse_dbg(psmouse,
1357 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001358 psmouse->packet[0], priv->mask0, priv->byte0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Seth Forsheeb46615f2011-11-07 19:53:30 -08001362 /* Bytes 2 - pktsize should have 0 in the highest bit */
Pali Rohára7ef82a2014-11-08 23:36:09 -08001363 if (priv->proto_version < ALPS_PROTO_V5 &&
Dave Turvene75af9e52013-02-21 22:58:28 -08001364 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001365 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001366 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1367 psmouse->pktcnt - 1,
1368 psmouse->packet[psmouse->pktcnt - 1]);
Pali Rohára7ef82a2014-11-08 23:36:09 -08001369
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08001370 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
Pali Rohára7ef82a2014-11-08 23:36:09 -08001371 psmouse->pktcnt == psmouse->pktsize) {
1372 /*
1373 * Some Dell boxes, such as Latitude E6440 or E7440
1374 * with closed lid, quite often smash last byte of
1375 * otherwise valid packet with 0xff. Given that the
1376 * next packet is very likely to be valid let's
1377 * report PSMOUSE_FULL_PACKET but not process data,
1378 * rather than reporting PSMOUSE_BAD_DATA and
1379 * filling the logs.
1380 */
1381 return PSMOUSE_FULL_PACKET;
1382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return PSMOUSE_BAD_DATA;
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Yunkang Tang38088432014-07-26 13:51:41 -07001387 if (priv->proto_version == ALPS_PROTO_V7 &&
1388 !alps_is_valid_package_v7(psmouse)) {
1389 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1390 psmouse->pktcnt - 1,
1391 psmouse->packet[psmouse->pktcnt - 1]);
1392 return PSMOUSE_BAD_DATA;
1393 }
1394
Seth Forsheeb46615f2011-11-07 19:53:30 -08001395 if (psmouse->pktcnt == psmouse->pktsize) {
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08001396 priv->process_packet(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return PSMOUSE_FULL_PACKET;
1398 }
1399
1400 return PSMOUSE_GOOD_DATA;
1401}
1402
Seth Forshee25bded72011-11-07 19:53:36 -08001403static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1404{
1405 struct ps2dev *ps2dev = &psmouse->ps2dev;
1406 struct alps_data *priv = psmouse->private;
1407 int command;
1408 unsigned char *param;
1409 unsigned char dummy[4];
1410
1411 BUG_ON(nibble > 0xf);
1412
1413 command = priv->nibble_commands[nibble].command;
1414 param = (command & 0x0f00) ?
1415 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1416
1417 if (ps2_command(ps2dev, param, command))
1418 return -1;
1419
1420 return 0;
1421}
1422
1423static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1424{
1425 struct ps2dev *ps2dev = &psmouse->ps2dev;
1426 struct alps_data *priv = psmouse->private;
1427 int i, nibble;
1428
1429 if (ps2_command(ps2dev, NULL, priv->addr_command))
1430 return -1;
1431
1432 for (i = 12; i >= 0; i -= 4) {
1433 nibble = (addr >> i) & 0xf;
1434 if (alps_command_mode_send_nibble(psmouse, nibble))
1435 return -1;
1436 }
1437
1438 return 0;
1439}
1440
1441static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1442{
1443 struct ps2dev *ps2dev = &psmouse->ps2dev;
1444 unsigned char param[4];
1445
1446 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1447 return -1;
1448
1449 /*
1450 * The address being read is returned in the first two bytes
1451 * of the result. Check that this address matches the expected
1452 * address.
1453 */
1454 if (addr != ((param[0] << 8) | param[1]))
1455 return -1;
1456
1457 return param[2];
1458}
1459
1460static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1461{
1462 if (alps_command_mode_set_addr(psmouse, addr))
1463 return -1;
1464 return __alps_command_mode_read_reg(psmouse, addr);
1465}
1466
1467static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1468{
1469 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1470 return -1;
1471 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1472 return -1;
1473 return 0;
1474}
1475
1476static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1477 u8 value)
1478{
1479 if (alps_command_mode_set_addr(psmouse, addr))
1480 return -1;
1481 return __alps_command_mode_write_reg(psmouse, value);
1482}
1483
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001484static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1485 int repeated_command, unsigned char *param)
1486{
1487 struct ps2dev *ps2dev = &psmouse->ps2dev;
1488
1489 param[0] = 0;
1490 if (init_command && ps2_command(ps2dev, param, init_command))
1491 return -EIO;
1492
1493 if (ps2_command(ps2dev, NULL, repeated_command) ||
1494 ps2_command(ps2dev, NULL, repeated_command) ||
1495 ps2_command(ps2dev, NULL, repeated_command))
1496 return -EIO;
1497
1498 param[0] = param[1] = param[2] = 0xff;
1499 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1500 return -EIO;
1501
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001502 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1503 repeated_command, param);
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001504 return 0;
1505}
1506
Yunkang Tang38088432014-07-26 13:51:41 -07001507static bool alps_check_valid_firmware_id(unsigned char id[])
1508{
1509 if (id[0] == 0x73)
1510 return true;
1511
1512 if (id[0] == 0x88 &&
1513 (id[1] == 0x07 ||
1514 id[1] == 0x08 ||
1515 (id[1] & 0xf0) == 0xb0 ||
1516 (id[1] & 0xf0) == 0xc0)) {
1517 return true;
1518 }
1519
1520 return false;
1521}
1522
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001523static int alps_enter_command_mode(struct psmouse *psmouse)
Seth Forshee25bded72011-11-07 19:53:36 -08001524{
1525 unsigned char param[4];
Seth Forshee25bded72011-11-07 19:53:36 -08001526
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001527 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001528 psmouse_err(psmouse, "failed to enter command mode\n");
1529 return -1;
1530 }
1531
Yunkang Tang38088432014-07-26 13:51:41 -07001532 if (!alps_check_valid_firmware_id(param)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001533 psmouse_dbg(psmouse,
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001534 "unknown response while entering command mode\n");
Seth Forshee25bded72011-11-07 19:53:36 -08001535 return -1;
1536 }
Seth Forshee25bded72011-11-07 19:53:36 -08001537 return 0;
1538}
1539
1540static inline int alps_exit_command_mode(struct psmouse *psmouse)
1541{
1542 struct ps2dev *ps2dev = &psmouse->ps2dev;
1543 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1544 return -1;
1545 return 0;
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/*
1549 * For DualPoint devices select the device that should respond to
1550 * subsequent commands. It looks like glidepad is behind stickpointer,
1551 * I'd thought it would be other way around...
1552 */
Seth Forshee25bded72011-11-07 19:53:36 -08001553static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct ps2dev *ps2dev = &psmouse->ps2dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1557
1558 if (ps2_command(ps2dev, NULL, cmd) ||
1559 ps2_command(ps2dev, NULL, cmd) ||
1560 ps2_command(ps2dev, NULL, cmd) ||
1561 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1562 return -1;
1563
1564 /* we may get 3 more bytes, just ignore them */
Dmitry Torokhovc6117632005-06-01 02:39:51 -05001565 ps2_drain(ps2dev, 3, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 return 0;
1568}
1569
Seth Forshee25bded72011-11-07 19:53:36 -08001570static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct ps2dev *ps2dev = &psmouse->ps2dev;
1573
1574 /* Try ALPS magic knock - 4 disable before enable */
1575 if (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_DISABLE) ||
1579 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1580 return -1;
1581
1582 /*
1583 * Switch mouse to poll (remote) mode so motion data will not
1584 * get in our way
1585 */
1586 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1587}
1588
Yunkang Tang95f75e92013-12-01 22:33:52 -08001589static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1590{
1591 int i, nibble;
1592
1593 /*
1594 * b0-b11 are valid bits, send sequence is inverse.
1595 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1596 */
1597 for (i = 0; i <= 8; i += 4) {
1598 nibble = (word >> i) & 0xf;
1599 if (alps_command_mode_send_nibble(psmouse, nibble))
1600 return -1;
1601 }
1602
1603 return 0;
1604}
1605
1606static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1607 u16 addr, u16 value)
1608{
1609 struct ps2dev *ps2dev = &psmouse->ps2dev;
1610
1611 /* 0x0A0 is the command to write the word */
1612 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1613 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1614 alps_monitor_mode_send_word(psmouse, addr) ||
1615 alps_monitor_mode_send_word(psmouse, value) ||
1616 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1617 return -1;
1618
1619 return 0;
1620}
1621
1622static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1623{
1624 struct ps2dev *ps2dev = &psmouse->ps2dev;
1625
1626 if (enable) {
1627 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1628 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1629 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1630 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1631 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1632 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1633 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1634 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1635 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1636 return -1;
1637 } else {
1638 /* EC to exit monitor mode */
1639 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1640 return -1;
1641 }
1642
1643 return 0;
1644}
1645
1646static int alps_absolute_mode_v6(struct psmouse *psmouse)
1647{
1648 u16 reg_val = 0x181;
1649 int ret = -1;
1650
1651 /* enter monitor mode, to write the register */
1652 if (alps_monitor_mode(psmouse, true))
1653 return -1;
1654
1655 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1656
1657 if (alps_monitor_mode(psmouse, false))
1658 ret = -1;
1659
1660 return ret;
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static int alps_get_status(struct psmouse *psmouse, char *param)
1664{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
Kevin Cernekee24ba9702013-02-13 22:19:01 -08001666 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return -1;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670}
1671
1672/*
1673 * Turn touchpad tapping on or off. The sequences are:
1674 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1675 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1676 * My guess that 0xE9 (GetInfo) is here as a sync point.
1677 * For models that also have stickpointer (DualPoints) its tapping
1678 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1679 * we don't fiddle with it.
1680 */
1681static int alps_tap_mode(struct psmouse *psmouse, int enable)
1682{
1683 struct ps2dev *ps2dev = &psmouse->ps2dev;
1684 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1685 unsigned char tap_arg = enable ? 0x0A : 0x00;
1686 unsigned char param[4];
1687
1688 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1689 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1690 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1691 ps2_command(ps2dev, &tap_arg, cmd))
1692 return -1;
1693
1694 if (alps_get_status(psmouse, param))
1695 return -1;
1696
1697 return 0;
1698}
1699
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001700/*
1701 * alps_poll() - poll the touchpad for current motion packet.
1702 * Used in resync.
1703 */
1704static int alps_poll(struct psmouse *psmouse)
1705{
1706 struct alps_data *priv = psmouse->private;
Seth Forsheeb46615f2011-11-07 19:53:30 -08001707 unsigned char buf[sizeof(psmouse->packet)];
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001708 bool poll_failed;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001709
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001710 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001711 alps_passthrough_mode_v2(psmouse, true);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001712
1713 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1714 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1715
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001716 if (priv->flags & ALPS_PASS)
Seth Forshee25bded72011-11-07 19:53:36 -08001717 alps_passthrough_mode_v2(psmouse, false);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001718
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001719 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001720 return -1;
1721
1722 if ((psmouse->badbyte & 0xc8) == 0x08) {
1723/*
1724 * Poll the track stick ...
1725 */
1726 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1727 return -1;
1728 }
1729
1730 memcpy(psmouse->packet, buf, sizeof(buf));
1731 return 0;
1732}
1733
Seth Forshee25bded72011-11-07 19:53:36 -08001734static int alps_hw_init_v1_v2(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct alps_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001738 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001739 alps_passthrough_mode_v2(psmouse, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001743 if (alps_tap_mode(psmouse, true)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001744 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return -1;
Peter Osterlund963f6262005-07-11 01:08:04 -05001746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Seth Forshee25bded72011-11-07 19:53:36 -08001748 if (alps_absolute_mode_v1_v2(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001749 psmouse_err(psmouse, "Failed to enable absolute mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return -1;
1751 }
1752
Kevin Cernekee99df65e2013-02-13 20:56:33 -08001753 if ((priv->flags & ALPS_PASS) &&
Seth Forshee25bded72011-11-07 19:53:36 -08001754 alps_passthrough_mode_v2(psmouse, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return -1;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001758 /* ALPS needs stream mode, otherwise it won't report any data */
1759 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001760 psmouse_err(psmouse, "Failed to enable stream mode\n");
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04001761 return -1;
1762 }
1763
1764 return 0;
1765}
1766
Yunkang Tang95f75e92013-12-01 22:33:52 -08001767static int alps_hw_init_v6(struct psmouse *psmouse)
1768{
1769 unsigned char param[2] = {0xC8, 0x14};
1770
1771 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1772 if (alps_passthrough_mode_v2(psmouse, true))
1773 return -1;
1774
1775 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1776 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1777 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1778 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1779 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1780 return -1;
1781
1782 if (alps_passthrough_mode_v2(psmouse, false))
1783 return -1;
1784
1785 if (alps_absolute_mode_v6(psmouse)) {
1786 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1787 return -1;
1788 }
1789
1790 return 0;
1791}
1792
Seth Forshee25bded72011-11-07 19:53:36 -08001793/*
Kevin Cernekeecd401202013-02-13 22:28:07 -08001794 * Enable or disable passthrough mode to the trackstick.
Seth Forshee25bded72011-11-07 19:53:36 -08001795 */
Kevin Cernekeecd401202013-02-13 22:28:07 -08001796static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1797 int reg_base, bool enable)
Seth Forshee25bded72011-11-07 19:53:36 -08001798{
Kevin Cernekeecd401202013-02-13 22:28:07 -08001799 int reg_val, ret = -1;
Seth Forshee25bded72011-11-07 19:53:36 -08001800
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001801 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08001802 return -1;
1803
Kevin Cernekeecd401202013-02-13 22:28:07 -08001804 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1805 if (reg_val == -1)
1806 goto error;
1807
Seth Forshee25bded72011-11-07 19:53:36 -08001808 if (enable)
1809 reg_val |= 0x01;
1810 else
1811 reg_val &= ~0x01;
1812
Kevin Cernekeecd401202013-02-13 22:28:07 -08001813 ret = __alps_command_mode_write_reg(psmouse, reg_val);
Seth Forshee25bded72011-11-07 19:53:36 -08001814
Kevin Cernekeecd401202013-02-13 22:28:07 -08001815error:
1816 if (alps_exit_command_mode(psmouse))
1817 ret = -1;
1818 return ret;
Seth Forshee25bded72011-11-07 19:53:36 -08001819}
1820
1821/* Must be in command mode when calling this function */
1822static int alps_absolute_mode_v3(struct psmouse *psmouse)
1823{
1824 int reg_val;
1825
1826 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1827 if (reg_val == -1)
1828 return -1;
1829
1830 reg_val |= 0x06;
1831 if (__alps_command_mode_write_reg(psmouse, reg_val))
1832 return -1;
1833
1834 return 0;
1835}
1836
Kevin Cernekeecd401202013-02-13 22:28:07 -08001837static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1838{
1839 int ret = -EIO, reg_val;
1840
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001841 if (alps_enter_command_mode(psmouse))
Kevin Cernekeecd401202013-02-13 22:28:07 -08001842 goto error;
1843
1844 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1845 if (reg_val == -1)
1846 goto error;
1847
1848 /* bit 7: trackstick is present */
1849 ret = reg_val & 0x80 ? 0 : -ENODEV;
1850
1851error:
1852 alps_exit_command_mode(psmouse);
1853 return ret;
1854}
1855
1856static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1857{
1858 struct ps2dev *ps2dev = &psmouse->ps2dev;
1859 int ret = 0;
1860 unsigned char param[4];
1861
1862 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1863 return -EIO;
1864
1865 /*
1866 * E7 report for the trackstick
1867 *
1868 * There have been reports of failures to seem to trace back
1869 * to the above trackstick check failing. When these occur
1870 * this E7 report fails, so when that happens we continue
1871 * with the assumption that there isn't a trackstick after
1872 * all.
1873 */
1874 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
Dmitry Torokhova09221e2015-01-13 20:46:09 -08001875 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
Kevin Cernekeecd401202013-02-13 22:28:07 -08001876 ret = -ENODEV;
1877 } else {
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001878 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
Kevin Cernekeecd401202013-02-13 22:28:07 -08001879
1880 /*
1881 * Not sure what this does, but it is absolutely
1882 * essential. Without it, the touchpad does not
1883 * work at all and the trackstick just emits normal
1884 * PS/2 packets.
1885 */
1886 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1887 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1888 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1889 alps_command_mode_send_nibble(psmouse, 0x9) ||
1890 alps_command_mode_send_nibble(psmouse, 0x4)) {
1891 psmouse_err(psmouse,
1892 "Error sending magic E6 sequence\n");
1893 ret = -EIO;
1894 goto error;
1895 }
1896
1897 /*
1898 * This ensures the trackstick packets are in the format
1899 * supported by this driver. If bit 1 isn't set the packet
1900 * format is different.
1901 */
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001902 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001903 alps_command_mode_write_reg(psmouse,
1904 reg_base + 0x08, 0x82) ||
1905 alps_exit_command_mode(psmouse))
1906 ret = -EIO;
1907 }
1908
1909error:
1910 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1911 ret = -EIO;
1912
1913 return ret;
1914}
1915
Seth Forshee25bded72011-11-07 19:53:36 -08001916static int alps_hw_init_v3(struct psmouse *psmouse)
1917{
Seth Forshee25bded72011-11-07 19:53:36 -08001918 struct ps2dev *ps2dev = &psmouse->ps2dev;
1919 int reg_val;
1920 unsigned char param[4];
1921
Kevin Cernekeecd401202013-02-13 22:28:07 -08001922 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1923 if (reg_val == -EIO)
1924 goto error;
Dmitry Torokhov39fbe582013-02-14 09:04:24 -08001925
Kevin Cernekeecd401202013-02-13 22:28:07 -08001926 if (reg_val == 0 &&
1927 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
Seth Forshee25bded72011-11-07 19:53:36 -08001928 goto error;
1929
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08001930 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekeecd401202013-02-13 22:28:07 -08001931 alps_absolute_mode_v3(psmouse)) {
Seth Forshee25bded72011-11-07 19:53:36 -08001932 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1933 goto error;
1934 }
1935
1936 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1937 if (reg_val == -1)
1938 goto error;
1939 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1940 goto error;
1941
1942 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1943 if (reg_val == -1)
1944 goto error;
1945 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1946 goto error;
1947
1948 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1949 goto error;
1950 if (__alps_command_mode_write_reg(psmouse, 0x04))
1951 goto error;
1952
1953 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1954 goto error;
1955 if (__alps_command_mode_write_reg(psmouse, 0x03))
1956 goto error;
1957
1958 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1959 goto error;
1960 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1961 goto error;
1962
1963 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1964 goto error;
1965 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1966 goto error;
1967
Seth Forshee25bded72011-11-07 19:53:36 -08001968 alps_exit_command_mode(psmouse);
1969
1970 /* Set rate and enable data reporting */
1971 param[0] = 0x64;
1972 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1973 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1974 psmouse_err(psmouse, "Failed to enable data reporting\n");
1975 return -1;
1976 }
1977
1978 return 0;
1979
Seth Forshee25bded72011-11-07 19:53:36 -08001980error:
1981 /*
1982 * Leaving the touchpad in command mode will essentially render
1983 * it unusable until the machine reboots, so exit it here just
1984 * to be safe
1985 */
1986 alps_exit_command_mode(psmouse);
1987 return -1;
1988}
1989
Hans de Goedef3f33c62014-07-29 11:22:07 -07001990static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1991{
1992 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1993 struct alps_data *priv = psmouse->private;
1994
1995 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1996 if (reg < 0)
1997 return reg;
1998
1999 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2000 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2001
2002 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2003 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2004
2005 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2006 if (reg < 0)
2007 return reg;
2008
2009 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2010 x_electrode = 17 + x_electrode;
2011
2012 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2013 y_electrode = 13 + y_electrode;
2014
2015 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2016 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2017
2018 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2019 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2020
2021 psmouse_dbg(psmouse,
2022 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2023 x_pitch, y_pitch, x_electrode, y_electrode,
2024 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2025
2026 return 0;
2027}
2028
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002029static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2030{
Kevin Cernekeecd401202013-02-13 22:28:07 -08002031 struct alps_data *priv = psmouse->private;
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002032 struct ps2dev *ps2dev = &psmouse->ps2dev;
2033 int reg_val, ret = -1;
2034
Kevin Cernekeecd401202013-02-13 22:28:07 -08002035 if (priv->flags & ALPS_DUALPOINT) {
2036 reg_val = alps_setup_trackstick_v3(psmouse,
2037 ALPS_REG_BASE_RUSHMORE);
2038 if (reg_val == -EIO)
2039 goto error;
Kevin Cernekeecd401202013-02-13 22:28:07 -08002040 }
2041
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002042 if (alps_enter_command_mode(psmouse) ||
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002043 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2044 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2045 goto error;
2046
Hans de Goedef3f33c62014-07-29 11:22:07 -07002047 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2048 goto error;
2049
Kevin Cernekee1302bac2013-02-13 22:27:08 -08002050 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2051 if (reg_val == -1)
2052 goto error;
2053 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2054 goto error;
2055
2056 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2057 goto error;
2058
2059 /* enter absolute mode */
2060 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2061 if (reg_val == -1)
2062 goto error;
2063 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2064 goto error;
2065
2066 alps_exit_command_mode(psmouse);
2067 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2068
2069error:
2070 alps_exit_command_mode(psmouse);
2071 return ret;
2072}
2073
Seth Forshee25bded72011-11-07 19:53:36 -08002074/* Must be in command mode when calling this function */
2075static int alps_absolute_mode_v4(struct psmouse *psmouse)
2076{
2077 int reg_val;
2078
2079 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2080 if (reg_val == -1)
2081 return -1;
2082
2083 reg_val |= 0x02;
2084 if (__alps_command_mode_write_reg(psmouse, reg_val))
2085 return -1;
2086
2087 return 0;
2088}
2089
2090static int alps_hw_init_v4(struct psmouse *psmouse)
2091{
Seth Forshee25bded72011-11-07 19:53:36 -08002092 struct ps2dev *ps2dev = &psmouse->ps2dev;
2093 unsigned char param[4];
2094
Kevin Cernekeed18e53f2013-02-21 22:58:20 -08002095 if (alps_enter_command_mode(psmouse))
Seth Forshee25bded72011-11-07 19:53:36 -08002096 goto error;
2097
2098 if (alps_absolute_mode_v4(psmouse)) {
2099 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2100 goto error;
2101 }
2102
2103 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2104 goto error;
2105
2106 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2107 goto error;
2108
2109 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2110 goto error;
2111
2112 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2113 goto error;
2114
2115 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2116 goto error;
2117
2118 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2119 goto error;
2120
2121 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2122 goto error;
2123
2124 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2125 goto error;
2126
2127 alps_exit_command_mode(psmouse);
2128
2129 /*
2130 * This sequence changes the output from a 9-byte to an
2131 * 8-byte format. All the same data seems to be present,
2132 * just in a more compact format.
2133 */
2134 param[0] = 0xc8;
2135 param[1] = 0x64;
2136 param[2] = 0x50;
2137 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2138 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2139 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2140 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2141 return -1;
2142
2143 /* Set rate and enable data reporting */
2144 param[0] = 0x64;
2145 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2146 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2147 psmouse_err(psmouse, "Failed to enable data reporting\n");
2148 return -1;
2149 }
2150
2151 return 0;
2152
2153error:
2154 /*
2155 * Leaving the touchpad in command mode will essentially render
2156 * it unusable until the machine reboots, so exit it here just
2157 * to be safe
2158 */
2159 alps_exit_command_mode(psmouse);
2160 return -1;
2161}
2162
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002163static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2164 struct alps_data *priv)
2165{
2166 struct ps2dev *ps2dev = &psmouse->ps2dev;
2167 unsigned char param[4] = {0};
2168 int num_x_electrode, num_y_electrode;
2169
2170 if (alps_enter_command_mode(psmouse))
2171 return -1;
2172
2173 param[0] = 0x0a;
2174 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2175 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2176 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2177 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2178 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2179 return -1;
2180
2181 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2182 return -1;
2183
2184 /*
2185 * Dolphin's sensor line number is not fixed. It can be calculated
2186 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2187 * Further more, we can get device's x_max and y_max by multiplying
2188 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2189 *
2190 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2191 * real sensor line number X = 11 + 8 = 19, and
2192 * real sensor line number Y = 8 + 1 = 9.
2193 * So, x_max = (19 - 1) * 64 = 1152, and
2194 * y_max = (9 - 1) * 64 = 512.
2195 */
2196 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2197 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2198 priv->x_bits = num_x_electrode;
2199 priv->y_bits = num_y_electrode;
2200 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2201 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2202
2203 if (alps_exit_command_mode(psmouse))
2204 return -1;
2205
2206 return 0;
2207}
2208
Dave Turvene75af9e52013-02-21 22:58:28 -08002209static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2210{
2211 struct ps2dev *ps2dev = &psmouse->ps2dev;
2212 unsigned char param[2];
2213
2214 /* This is dolphin "v1" as empirically defined by florin9doi */
2215 param[0] = 0x64;
2216 param[1] = 0x28;
2217
2218 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2219 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2220 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2221 return -1;
2222
2223 return 0;
2224}
2225
Yunkang Tang38088432014-07-26 13:51:41 -07002226static int alps_hw_init_v7(struct psmouse *psmouse)
2227{
2228 struct ps2dev *ps2dev = &psmouse->ps2dev;
2229 int reg_val, ret = -1;
2230
2231 if (alps_enter_command_mode(psmouse) ||
2232 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2233 goto error;
2234
Hans de Goedef3f33c62014-07-29 11:22:07 -07002235 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2236 goto error;
2237
Yunkang Tang38088432014-07-26 13:51:41 -07002238 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2239 goto error;
2240
2241 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2242 if (reg_val == -1)
2243 goto error;
2244 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2245 goto error;
2246
2247 alps_exit_command_mode(psmouse);
2248 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2249
2250error:
2251 alps_exit_command_mode(psmouse);
2252 return ret;
2253}
2254
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002255static int alps_set_protocol(struct psmouse *psmouse,
2256 struct alps_data *priv,
2257 const struct alps_protocol_info *protocol)
Seth Forshee25bded72011-11-07 19:53:36 -08002258{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002259 psmouse->private = priv;
2260
2261 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2262
2263 priv->proto_version = protocol->version;
2264 priv->byte0 = protocol->byte0;
2265 priv->mask0 = protocol->mask0;
2266 priv->flags = protocol->flags;
Kevin Cernekeef673ceb2013-02-13 22:23:34 -08002267
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002268 priv->x_max = 2000;
2269 priv->y_max = 1400;
2270 priv->x_bits = 15;
2271 priv->y_bits = 11;
2272
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002273 switch (priv->proto_version) {
Seth Forshee25bded72011-11-07 19:53:36 -08002274 case ALPS_PROTO_V1:
2275 case ALPS_PROTO_V2:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002276 priv->hw_init = alps_hw_init_v1_v2;
2277 priv->process_packet = alps_process_packet_v1_v2;
2278 priv->set_abs_params = alps_set_abs_params_st;
Yunkang Tang95f75e92013-12-01 22:33:52 -08002279 priv->x_max = 1023;
2280 priv->y_max = 767;
Seth Forshee25bded72011-11-07 19:53:36 -08002281 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002282
Seth Forshee25bded72011-11-07 19:53:36 -08002283 case ALPS_PROTO_V3:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002284 priv->hw_init = alps_hw_init_v3;
2285 priv->process_packet = alps_process_packet_v3;
2286 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekeef85e5002013-02-13 22:26:11 -08002287 priv->decode_fields = alps_decode_pinnacle;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002288 priv->nibble_commands = alps_v3_nibble_commands;
2289 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Seth Forshee25bded72011-11-07 19:53:36 -08002290 break;
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002291
2292 case ALPS_PROTO_V3_RUSHMORE:
2293 priv->hw_init = alps_hw_init_rushmore_v3;
2294 priv->process_packet = alps_process_packet_v3;
2295 priv->set_abs_params = alps_set_abs_params_mt;
2296 priv->decode_fields = alps_decode_rushmore;
2297 priv->nibble_commands = alps_v3_nibble_commands;
2298 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2299 priv->x_bits = 16;
2300 priv->y_bits = 12;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002301
2302 if (alps_probe_trackstick_v3(psmouse,
2303 ALPS_REG_BASE_RUSHMORE) < 0)
2304 priv->flags &= ~ALPS_DUALPOINT;
2305
Dmitry Torokhovfb2dd7a2015-01-14 10:39:52 -08002306 break;
2307
Seth Forshee25bded72011-11-07 19:53:36 -08002308 case ALPS_PROTO_V4:
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002309 priv->hw_init = alps_hw_init_v4;
2310 priv->process_packet = alps_process_packet_v4;
2311 priv->set_abs_params = alps_set_abs_params_mt;
Kevin Cernekee50e8b212013-02-13 22:23:04 -08002312 priv->nibble_commands = alps_v4_nibble_commands;
2313 priv->addr_command = PSMOUSE_CMD_DISABLE;
Seth Forshee25bded72011-11-07 19:53:36 -08002314 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002315
Dave Turvene75af9e52013-02-21 22:58:28 -08002316 case ALPS_PROTO_V5:
2317 priv->hw_init = alps_hw_init_dolphin_v1;
Yunkang Tangee65d4b2013-12-26 14:54:19 -08002318 priv->process_packet = alps_process_touchpad_packet_v3_v5;
Dave Turvene75af9e52013-02-21 22:58:28 -08002319 priv->decode_fields = alps_decode_dolphin;
2320 priv->set_abs_params = alps_set_abs_params_mt;
2321 priv->nibble_commands = alps_v3_nibble_commands;
2322 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dave Turvene75af9e52013-02-21 22:58:28 -08002323 priv->x_max = 1360;
2324 priv->y_max = 660;
2325 priv->x_bits = 23;
2326 priv->y_bits = 12;
2327 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002328
Yunkang Tang95f75e92013-12-01 22:33:52 -08002329 case ALPS_PROTO_V6:
2330 priv->hw_init = alps_hw_init_v6;
2331 priv->process_packet = alps_process_packet_v6;
2332 priv->set_abs_params = alps_set_abs_params_st;
2333 priv->nibble_commands = alps_v6_nibble_commands;
2334 priv->x_max = 2047;
2335 priv->y_max = 1535;
2336 break;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002337
Yunkang Tang38088432014-07-26 13:51:41 -07002338 case ALPS_PROTO_V7:
2339 priv->hw_init = alps_hw_init_v7;
2340 priv->process_packet = alps_process_packet_v7;
2341 priv->decode_fields = alps_decode_packet_v7;
2342 priv->set_abs_params = alps_set_abs_params_mt;
2343 priv->nibble_commands = alps_v3_nibble_commands;
2344 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002345
2346 if (alps_dolphin_get_device_area(psmouse, priv))
2347 return -EIO;
Yunkang Tang38088432014-07-26 13:51:41 -07002348
2349 if (priv->fw_ver[1] != 0xba)
2350 priv->flags |= ALPS_BUTTONPAD;
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002351
Yunkang Tang38088432014-07-26 13:51:41 -07002352 break;
Seth Forshee25bded72011-11-07 19:53:36 -08002353 }
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002354
2355 return 0;
Seth Forshee25bded72011-11-07 19:53:36 -08002356}
2357
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002358static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2359 unsigned char *ec)
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002360{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002361 const struct alps_model_info *model;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002362 int i;
2363
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002364 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2365 model = &alps_model_data[i];
2366
2367 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2368 (!model->command_mode_resp ||
2369 model->command_mode_resp == ec[2])) {
2370
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002371 return &model->protocol_info;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002372 }
2373 }
2374
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002375 return NULL;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002376}
2377
2378static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2379{
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002380 const struct alps_protocol_info *protocol;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002381 unsigned char e6[4], e7[4], ec[4];
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002382 int error;
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002383
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002384 /*
2385 * First try "E6 report".
2386 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2387 * The bits 0-2 of the first byte will be 1s if some buttons are
2388 * pressed.
2389 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002390 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2391 PSMOUSE_CMD_SETSCALE11, e6))
2392 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002393
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002394 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2395 return -EINVAL;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002396
2397 /*
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002398 * Now get the "E7" and "EC" reports. These will uniquely identify
2399 * most ALPS touchpads.
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002400 */
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002401 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2402 PSMOUSE_CMD_SETSCALE21, e7) ||
2403 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2404 PSMOUSE_CMD_RESET_WRAP, ec) ||
2405 alps_exit_command_mode(psmouse))
2406 return -EIO;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002407
Dmitry Torokhov3296f712015-01-12 00:30:32 -08002408 protocol = alps_match_table(e7, ec);
2409 if (!protocol) {
2410 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2411 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2412 protocol = &alps_v5_protocol_data;
2413 } else if (ec[0] == 0x88 &&
2414 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2415 protocol = &alps_v7_protocol_data;
2416 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2417 protocol = &alps_v3_rushmore_data;
2418 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2419 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2420 protocol = &alps_v3_protocol_data;
2421 } else {
2422 psmouse_dbg(psmouse,
2423 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2424 return -EINVAL;
2425 }
2426 }
2427
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002428 if (priv) {
2429 /* Save the Firmware version */
2430 memcpy(priv->fw_ver, ec, 3);
2431 error = alps_set_protocol(psmouse, priv, protocol);
2432 if (error)
2433 return error;
2434 }
Hans de Goedec0cd17f2014-07-25 22:49:14 -07002435
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002436 return 0;
Kevin Cernekee2e992cc2013-02-13 20:57:04 -08002437}
2438
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002439static int alps_reconnect(struct psmouse *psmouse)
2440{
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002441 struct alps_data *priv = psmouse->private;
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002442
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002443 psmouse_reset(psmouse);
2444
Kevin Cernekeeb5d6b852013-02-13 22:19:59 -08002445 if (alps_identify(psmouse, priv) < 0)
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002446 return -1;
2447
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002448 return priv->hw_init(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451static void alps_disconnect(struct psmouse *psmouse)
2452{
2453 struct alps_data *priv = psmouse->private;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 psmouse_reset(psmouse);
Sebastian Kapfer1d9f2622009-12-15 08:39:50 -08002456 del_timer_sync(&priv->timer);
Pali Rohár04aae282015-01-14 13:18:30 -08002457 if (priv->dev2)
2458 input_unregister_device(priv->dev2);
2459 if (!IS_ERR_OR_NULL(priv->dev3))
2460 input_unregister_device(priv->dev3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 kfree(priv);
2462}
2463
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002464static void alps_set_abs_params_st(struct alps_data *priv,
2465 struct input_dev *dev1)
2466{
Yunkang Tang95f75e92013-12-01 22:33:52 -08002467 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2468 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002469}
2470
2471static void alps_set_abs_params_mt(struct alps_data *priv,
2472 struct input_dev *dev1)
2473{
Kevin Cernekee7a9f73e2013-02-13 22:24:55 -08002474 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2475 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002476
Hans de Goedef3f33c62014-07-29 11:22:07 -07002477 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2478 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2479
Hans de Goedecdf333e2014-07-25 22:44:42 -07002480 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2481 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2482
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002483 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2484 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
Yunkang Tang38088432014-07-26 13:51:41 -07002485
2486 /* V7 is real multi-touch */
2487 if (priv->proto_version == ALPS_PROTO_V7)
2488 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491int alps_init(struct psmouse *psmouse)
2492{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002493 struct alps_data *priv = psmouse->private;
Pali Rohár04aae282015-01-14 13:18:30 -08002494 struct input_dev *dev1 = psmouse->dev;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002495 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002497 error = priv->hw_init(psmouse);
2498 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto init_fail;
2500
Dmitry Torokhov7105d2e2009-12-11 23:54:54 -08002501 /*
2502 * Undo part of setup done for us by psmouse core since touchpad
2503 * is not a relative device.
2504 */
2505 __clear_bit(EV_REL, dev1->evbit);
2506 __clear_bit(REL_X, dev1->relbit);
2507 __clear_bit(REL_Y, dev1->relbit);
2508
2509 /*
2510 * Now set up our capabilities.
2511 */
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002512 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2513 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2514 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002515 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2516 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002518 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
Seth Forshee25bded72011-11-07 19:53:36 -08002519
Kevin Cernekee24af5cb2013-02-13 22:22:08 -08002520 priv->set_abs_params(priv, dev1);
Yunkang Tang38088432014-07-26 13:51:41 -07002521 /* No pressure on V7 */
2522 if (priv->proto_version != ALPS_PROTO_V7)
2523 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002525 if (priv->flags & ALPS_WHEEL) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002526 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2527 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002530 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -07002531 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2532 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002535 if (priv->flags & ALPS_FOUR_BUTTONS) {
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002536 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2537 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2538 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2539 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
Yunkang Tang38088432014-07-26 13:51:41 -07002540 } else if (priv->flags & ALPS_BUTTONPAD) {
2541 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2542 clear_bit(BTN_RIGHT, dev1->keybit);
Maxim Levitsky71bb21b2009-11-16 22:12:22 -08002543 } else {
2544 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2545 }
2546
Pali Rohárdfba8602015-01-09 12:48:44 -08002547 if (priv->flags & ALPS_DUALPOINT) {
Pali Rohár04aae282015-01-14 13:18:30 -08002548 struct input_dev *dev2;
2549
2550 dev2 = input_allocate_device();
2551 if (!dev2) {
2552 psmouse_err(psmouse,
2553 "failed to allocate trackstick device\n");
2554 error = -ENOMEM;
2555 goto init_fail;
2556 }
2557
2558 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2559 psmouse->ps2dev.serio->phys);
2560 dev2->phys = priv->phys2;
2561
Pali Rohárdfba8602015-01-09 12:48:44 -08002562 /*
2563 * format of input device name is: "protocol vendor name"
2564 * see function psmouse_switch_protocol() in psmouse-base.c
2565 */
2566 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
Pali Rohár04aae282015-01-14 13:18:30 -08002567
2568 dev2->id.bustype = BUS_I8042;
2569 dev2->id.vendor = 0x0002;
Pali Rohárdfba8602015-01-09 12:48:44 -08002570 dev2->id.product = PSMOUSE_ALPS;
2571 dev2->id.version = priv->proto_version;
Pali Rohár04aae282015-01-14 13:18:30 -08002572 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
Pali Rohárdfba8602015-01-09 12:48:44 -08002573
Pali Rohár04aae282015-01-14 13:18:30 -08002574 input_set_capability(dev2, EV_REL, REL_X);
2575 input_set_capability(dev2, EV_REL, REL_Y);
2576 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2577 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2578 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002579
Pali Rohár04aae282015-01-14 13:18:30 -08002580 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
Hans de Goede76113922014-09-08 14:42:12 -07002581 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2582
Pali Rohár04aae282015-01-14 13:18:30 -08002583 error = input_register_device(dev2);
2584 if (error) {
2585 psmouse_err(psmouse,
2586 "failed to register trackstick device: %d\n",
2587 error);
2588 input_free_device(dev2);
2589 goto init_fail;
2590 }
2591
2592 priv->dev2 = dev2;
2593 }
2594
2595 priv->psmouse = psmouse;
2596
2597 INIT_DELAYED_WORK(&priv->dev3_register_work,
2598 alps_register_bare_ps2_mouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 psmouse->protocol_handler = alps_process_byte;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002601 psmouse->poll = alps_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 psmouse->disconnect = alps_disconnect;
2603 psmouse->reconnect = alps_reconnect;
Kevin Cernekee99df65e2013-02-13 20:56:33 -08002604 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002606 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2607 psmouse->resync_time = 0;
2608
Pali Rohár9d720b32014-11-08 12:58:57 -08002609 /* Allow 2 invalid packets without resetting device */
2610 psmouse->resetafter = psmouse->pktsize * 2;
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return 0;
2613
2614init_fail:
Dmitry Torokhovf42649e2007-04-12 01:31:13 -04002615 psmouse_reset(psmouse);
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002616 /*
2617 * Even though we did not allocate psmouse->private we do free
2618 * it here.
2619 */
2620 kfree(psmouse->private);
Dmitry Torokhov1e0c5b12007-05-14 23:51:54 -04002621 psmouse->private = NULL;
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07002625int alps_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002627 struct alps_data *priv;
2628 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002630 error = alps_identify(psmouse, NULL);
2631 if (error)
2632 return error;
2633
2634 /*
2635 * Reset the device to make sure it is fully operational:
2636 * on some laptops, like certain Dell Latitudes, we may
2637 * fail to properly detect presence of trackstick if device
2638 * has not been reset.
2639 */
2640 psmouse_reset(psmouse);
2641
2642 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2643 if (!priv)
2644 return -ENOMEM;
2645
2646 error = alps_identify(psmouse, priv);
2647 if (error)
2648 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 if (set_properties) {
2651 psmouse->vendor = "ALPS";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002652 psmouse->name = priv->flags & ALPS_DUALPOINT ?
Dmitry Torokhov968ac842005-05-29 02:28:29 -05002653 "DualPoint TouchPad" : "GlidePoint";
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002654 psmouse->model = priv->proto_version;
2655 } else {
2656 /*
2657 * Destroy alps_data structure we allocated earlier since
2658 * this was just a "trial run". Otherwise we'll keep it
2659 * to be used by alps_init() which has to be called if
2660 * we succeed and set_properties is true.
2661 */
2662 kfree(priv);
2663 psmouse->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
Dmitry Torokhova09221e2015-01-13 20:46:09 -08002665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return 0;
2667}
2668