Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * BYD TouchPad PS/2 mouse driver
4 *
5 * Copyright (C) 2015 Chris Diamand <chris@diamand.org>
6 * Copyright (C) 2015 Richard Pospesel
7 * Copyright (C) 2015 Tai Chi Minh Ralph Eastwood
8 * Copyright (C) 2015 Martin Wimpress
9 * Copyright (C) 2015 Jay Kuri
10 */
11
12#include <linux/delay.h>
13#include <linux/input.h>
14#include <linux/libps2.h>
15#include <linux/serio.h>
16#include <linux/slab.h>
17
18#include "psmouse.h"
19#include "byd.h"
20
21/* PS2 Bits */
22#define PS2_Y_OVERFLOW BIT_MASK(7)
23#define PS2_X_OVERFLOW BIT_MASK(6)
24#define PS2_Y_SIGN BIT_MASK(5)
25#define PS2_X_SIGN BIT_MASK(4)
26#define PS2_ALWAYS_1 BIT_MASK(3)
27#define PS2_MIDDLE BIT_MASK(2)
28#define PS2_RIGHT BIT_MASK(1)
29#define PS2_LEFT BIT_MASK(0)
30
31/*
32 * BYD pad constants
33 */
34
35/*
36 * True device resolution is unknown, however experiments show the
37 * resolution is about 111 units/mm.
38 * Absolute coordinate packets are in the range 0-255 for both X and Y
39 * we pick ABS_X/ABS_Y dimensions which are multiples of 256 and in
40 * the right ballpark given the touchpad's physical dimensions and estimate
41 * resolution per spec sheet, device active area dimensions are
42 * 101.6 x 60.1 mm.
43 */
44#define BYD_PAD_WIDTH 11264
45#define BYD_PAD_HEIGHT 6656
46#define BYD_PAD_RESOLUTION 111
47
48/*
49 * Given the above dimensions, relative packets velocity is in multiples of
50 * 1 unit / 11 milliseconds. We use this dt to estimate distance traveled
51 */
52#define BYD_DT 11
53/* Time in jiffies used to timeout various touch events (64 ms) */
54#define BYD_TOUCH_TIMEOUT msecs_to_jiffies(64)
55
56/* BYD commands reverse engineered from windows driver */
57
58/*
59 * Swipe gesture from off-pad to on-pad
60 * 0 : disable
61 * 1 : enable
62 */
63#define BYD_CMD_SET_OFFSCREEN_SWIPE 0x10cc
64/*
65 * Tap and drag delay time
66 * 0 : disable
67 * 1 - 8 : least to most delay
68 */
69#define BYD_CMD_SET_TAP_DRAG_DELAY_TIME 0x10cf
70/*
71 * Physical buttons function mapping
72 * 0 : enable
73 * 4 : normal
74 * 5 : left button custom command
75 * 6 : right button custom command
76 * 8 : disable
77 */
78#define BYD_CMD_SET_PHYSICAL_BUTTONS 0x10d0
79/*
80 * Absolute mode (1 byte X/Y resolution)
81 * 0 : disable
82 * 2 : enable
83 */
84#define BYD_CMD_SET_ABSOLUTE_MODE 0x10d1
85/*
86 * Two finger scrolling
87 * 1 : vertical
88 * 2 : horizontal
89 * 3 : vertical + horizontal
90 * 4 : disable
91 */
92#define BYD_CMD_SET_TWO_FINGER_SCROLL 0x10d2
93/*
94 * Handedness
95 * 1 : right handed
96 * 2 : left handed
97 */
98#define BYD_CMD_SET_HANDEDNESS 0x10d3
99/*
100 * Tap to click
101 * 1 : enable
102 * 2 : disable
103 */
104#define BYD_CMD_SET_TAP 0x10d4
105/*
106 * Tap and drag
107 * 1 : tap and hold to drag
108 * 2 : tap and hold to drag + lock
109 * 3 : disable
110 */
111#define BYD_CMD_SET_TAP_DRAG 0x10d5
112/*
113 * Touch sensitivity
114 * 1 - 7 : least to most sensitive
115 */
116#define BYD_CMD_SET_TOUCH_SENSITIVITY 0x10d6
117/*
118 * One finger scrolling
119 * 1 : vertical
120 * 2 : horizontal
121 * 3 : vertical + horizontal
122 * 4 : disable
123 */
124#define BYD_CMD_SET_ONE_FINGER_SCROLL 0x10d7
125/*
126 * One finger scrolling function
127 * 1 : free scrolling
128 * 2 : edge motion
129 * 3 : free scrolling + edge motion
130 * 4 : disable
131 */
132#define BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC 0x10d8
133/*
134 * Sliding speed
135 * 1 - 5 : slowest to fastest
136 */
137#define BYD_CMD_SET_SLIDING_SPEED 0x10da
138/*
139 * Edge motion
140 * 1 : disable
141 * 2 : enable when dragging
142 * 3 : enable when dragging and pointing
143 */
144#define BYD_CMD_SET_EDGE_MOTION 0x10db
145/*
146 * Left edge region size
147 * 0 - 7 : smallest to largest width
148 */
149#define BYD_CMD_SET_LEFT_EDGE_REGION 0x10dc
150/*
151 * Top edge region size
152 * 0 - 9 : smallest to largest height
153 */
154#define BYD_CMD_SET_TOP_EDGE_REGION 0x10dd
155/*
156 * Disregard palm press as clicks
157 * 1 - 6 : smallest to largest
158 */
159#define BYD_CMD_SET_PALM_CHECK 0x10de
160/*
161 * Right edge region size
162 * 0 - 7 : smallest to largest width
163 */
164#define BYD_CMD_SET_RIGHT_EDGE_REGION 0x10df
165/*
166 * Bottom edge region size
167 * 0 - 9 : smallest to largest height
168 */
169#define BYD_CMD_SET_BOTTOM_EDGE_REGION 0x10e1
170/*
171 * Multitouch gestures
172 * 1 : enable
173 * 2 : disable
174 */
175#define BYD_CMD_SET_MULTITOUCH 0x10e3
176/*
177 * Edge motion speed
178 * 0 : control with finger pressure
179 * 1 - 9 : slowest to fastest
180 */
181#define BYD_CMD_SET_EDGE_MOTION_SPEED 0x10e4
182/*
183 * Two finger scolling function
184 * 0 : free scrolling
185 * 1 : free scrolling (with momentum)
186 * 2 : edge motion
187 * 3 : free scrolling (with momentum) + edge motion
188 * 4 : disable
189 */
190#define BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC 0x10e5
191
192/*
193 * The touchpad generates a mixture of absolute and relative packets, indicated
194 * by the last byte of each packet being set to one of the following:
195 */
196#define BYD_PACKET_ABSOLUTE 0xf8
197#define BYD_PACKET_RELATIVE 0x00
198/* Multitouch gesture packets */
199#define BYD_PACKET_PINCH_IN 0xd8
200#define BYD_PACKET_PINCH_OUT 0x28
201#define BYD_PACKET_ROTATE_CLOCKWISE 0x29
202#define BYD_PACKET_ROTATE_ANTICLOCKWISE 0xd7
203#define BYD_PACKET_TWO_FINGER_SCROLL_RIGHT 0x2a
204#define BYD_PACKET_TWO_FINGER_SCROLL_DOWN 0x2b
205#define BYD_PACKET_TWO_FINGER_SCROLL_UP 0xd5
206#define BYD_PACKET_TWO_FINGER_SCROLL_LEFT 0xd6
207#define BYD_PACKET_THREE_FINGER_SWIPE_RIGHT 0x2c
208#define BYD_PACKET_THREE_FINGER_SWIPE_DOWN 0x2d
209#define BYD_PACKET_THREE_FINGER_SWIPE_UP 0xd3
210#define BYD_PACKET_THREE_FINGER_SWIPE_LEFT 0xd4
211#define BYD_PACKET_FOUR_FINGER_DOWN 0x33
212#define BYD_PACKET_FOUR_FINGER_UP 0xcd
213#define BYD_PACKET_REGION_SCROLL_RIGHT 0x35
214#define BYD_PACKET_REGION_SCROLL_DOWN 0x36
215#define BYD_PACKET_REGION_SCROLL_UP 0xca
216#define BYD_PACKET_REGION_SCROLL_LEFT 0xcb
217#define BYD_PACKET_RIGHT_CORNER_CLICK 0xd2
218#define BYD_PACKET_LEFT_CORNER_CLICK 0x2e
219#define BYD_PACKET_LEFT_AND_RIGHT_CORNER_CLICK 0x2f
220#define BYD_PACKET_ONTO_PAD_SWIPE_RIGHT 0x37
221#define BYD_PACKET_ONTO_PAD_SWIPE_DOWN 0x30
222#define BYD_PACKET_ONTO_PAD_SWIPE_UP 0xd0
223#define BYD_PACKET_ONTO_PAD_SWIPE_LEFT 0xc9
224
225struct byd_data {
226 struct timer_list timer;
227 struct psmouse *psmouse;
228 s32 abs_x;
229 s32 abs_y;
230 typeof(jiffies) last_touch_time;
231 bool btn_left;
232 bool btn_right;
233 bool touch;
234};
235
236static void byd_report_input(struct psmouse *psmouse)
237{
238 struct byd_data *priv = psmouse->private;
239 struct input_dev *dev = psmouse->dev;
240
241 input_report_key(dev, BTN_TOUCH, priv->touch);
242 input_report_key(dev, BTN_TOOL_FINGER, priv->touch);
243
244 input_report_abs(dev, ABS_X, priv->abs_x);
245 input_report_abs(dev, ABS_Y, priv->abs_y);
246 input_report_key(dev, BTN_LEFT, priv->btn_left);
247 input_report_key(dev, BTN_RIGHT, priv->btn_right);
248
249 input_sync(dev);
250}
251
252static void byd_clear_touch(struct timer_list *t)
253{
254 struct byd_data *priv = from_timer(priv, t, timer);
255 struct psmouse *psmouse = priv->psmouse;
256
257 serio_pause_rx(psmouse->ps2dev.serio);
258 priv->touch = false;
259
260 byd_report_input(psmouse);
261
262 serio_continue_rx(psmouse->ps2dev.serio);
263
264 /*
265 * Move cursor back to center of pad when we lose touch - this
266 * specifically improves user experience when moving cursor with one
267 * finger, and pressing a button with another.
268 */
269 priv->abs_x = BYD_PAD_WIDTH / 2;
270 priv->abs_y = BYD_PAD_HEIGHT / 2;
271}
272
273static psmouse_ret_t byd_process_byte(struct psmouse *psmouse)
274{
275 struct byd_data *priv = psmouse->private;
276 u8 *pkt = psmouse->packet;
277
278 if (psmouse->pktcnt > 0 && !(pkt[0] & PS2_ALWAYS_1)) {
279 psmouse_warn(psmouse, "Always_1 bit not 1. pkt[0] = %02x\n",
280 pkt[0]);
281 return PSMOUSE_BAD_DATA;
282 }
283
284 if (psmouse->pktcnt < psmouse->pktsize)
285 return PSMOUSE_GOOD_DATA;
286
287 /* Otherwise, a full packet has been received */
288 switch (pkt[3]) {
289 case BYD_PACKET_ABSOLUTE:
290 /* Only use absolute packets for the start of movement. */
291 if (!priv->touch) {
292 /* needed to detect tap */
293 typeof(jiffies) tap_time =
294 priv->last_touch_time + BYD_TOUCH_TIMEOUT;
295 priv->touch = time_after(jiffies, tap_time);
296
297 /* init abs position */
298 priv->abs_x = pkt[1] * (BYD_PAD_WIDTH / 256);
299 priv->abs_y = (255 - pkt[2]) * (BYD_PAD_HEIGHT / 256);
300 }
301 break;
302 case BYD_PACKET_RELATIVE: {
303 /* Standard packet */
304 /* Sign-extend if a sign bit is set. */
305 u32 signx = pkt[0] & PS2_X_SIGN ? ~0xFF : 0;
306 u32 signy = pkt[0] & PS2_Y_SIGN ? ~0xFF : 0;
307 s32 dx = signx | (int) pkt[1];
308 s32 dy = signy | (int) pkt[2];
309
310 /* Update position based on velocity */
311 priv->abs_x += dx * BYD_DT;
312 priv->abs_y -= dy * BYD_DT;
313
314 priv->touch = true;
315 break;
316 }
317 default:
318 psmouse_warn(psmouse,
319 "Unrecognized Z: pkt = %02x %02x %02x %02x\n",
320 psmouse->packet[0], psmouse->packet[1],
321 psmouse->packet[2], psmouse->packet[3]);
322 return PSMOUSE_BAD_DATA;
323 }
324
325 priv->btn_left = pkt[0] & PS2_LEFT;
326 priv->btn_right = pkt[0] & PS2_RIGHT;
327
328 byd_report_input(psmouse);
329
330 /* Reset time since last touch. */
331 if (priv->touch) {
332 priv->last_touch_time = jiffies;
333 mod_timer(&priv->timer, jiffies + BYD_TOUCH_TIMEOUT);
334 }
335
336 return PSMOUSE_FULL_PACKET;
337}
338
339static int byd_reset_touchpad(struct psmouse *psmouse)
340{
341 struct ps2dev *ps2dev = &psmouse->ps2dev;
342 u8 param[4];
343 size_t i;
344
345 static const struct {
346 u16 command;
347 u8 arg;
348 } seq[] = {
349 /*
350 * Intellimouse initialization sequence, to get 4-byte instead
351 * of 3-byte packets.
352 */
353 { PSMOUSE_CMD_SETRATE, 0xC8 },
354 { PSMOUSE_CMD_SETRATE, 0x64 },
355 { PSMOUSE_CMD_SETRATE, 0x50 },
356 { PSMOUSE_CMD_GETID, 0 },
357 { PSMOUSE_CMD_ENABLE, 0 },
358 /*
359 * BYD-specific initialization, which enables absolute mode and
360 * (if desired), the touchpad's built-in gesture detection.
361 */
362 { 0x10E2, 0x00 },
363 { 0x10E0, 0x02 },
364 /* The touchpad should reply with 4 seemingly-random bytes */
365 { 0x14E0, 0x01 },
366 /* Pairs of parameters and values. */
367 { BYD_CMD_SET_HANDEDNESS, 0x01 },
368 { BYD_CMD_SET_PHYSICAL_BUTTONS, 0x04 },
369 { BYD_CMD_SET_TAP, 0x02 },
370 { BYD_CMD_SET_ONE_FINGER_SCROLL, 0x04 },
371 { BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC, 0x04 },
372 { BYD_CMD_SET_EDGE_MOTION, 0x01 },
373 { BYD_CMD_SET_PALM_CHECK, 0x00 },
374 { BYD_CMD_SET_MULTITOUCH, 0x02 },
375 { BYD_CMD_SET_TWO_FINGER_SCROLL, 0x04 },
376 { BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC, 0x04 },
377 { BYD_CMD_SET_LEFT_EDGE_REGION, 0x00 },
378 { BYD_CMD_SET_TOP_EDGE_REGION, 0x00 },
379 { BYD_CMD_SET_RIGHT_EDGE_REGION, 0x00 },
380 { BYD_CMD_SET_BOTTOM_EDGE_REGION, 0x00 },
381 { BYD_CMD_SET_ABSOLUTE_MODE, 0x02 },
382 /* Finalize initialization. */
383 { 0x10E0, 0x00 },
384 { 0x10E2, 0x01 },
385 };
386
387 for (i = 0; i < ARRAY_SIZE(seq); ++i) {
388 memset(param, 0, sizeof(param));
389 param[0] = seq[i].arg;
390 if (ps2_command(ps2dev, param, seq[i].command))
391 return -EIO;
392 }
393
394 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
395 return 0;
396}
397
398static int byd_reconnect(struct psmouse *psmouse)
399{
400 int retry = 0, error = 0;
401
402 psmouse_dbg(psmouse, "Reconnect\n");
403 do {
404 psmouse_reset(psmouse);
405 if (retry)
406 ssleep(1);
407 error = byd_detect(psmouse, 0);
408 } while (error && ++retry < 3);
409
410 if (error)
411 return error;
412
413 psmouse_dbg(psmouse, "Reconnected after %d attempts\n", retry);
414
415 error = byd_reset_touchpad(psmouse);
416 if (error) {
417 psmouse_err(psmouse, "Unable to initialize device\n");
418 return error;
419 }
420
421 return 0;
422}
423
424static void byd_disconnect(struct psmouse *psmouse)
425{
426 struct byd_data *priv = psmouse->private;
427
428 if (priv) {
429 del_timer(&priv->timer);
430 kfree(psmouse->private);
431 psmouse->private = NULL;
432 }
433}
434
435int byd_detect(struct psmouse *psmouse, bool set_properties)
436{
437 struct ps2dev *ps2dev = &psmouse->ps2dev;
438 u8 param[4] = {0x03, 0x00, 0x00, 0x00};
439
440 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
441 return -1;
442 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
443 return -1;
444 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
445 return -1;
446 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
447 return -1;
448 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
449 return -1;
450
451 if (param[1] != 0x03 || param[2] != 0x64)
452 return -ENODEV;
453
454 psmouse_dbg(psmouse, "BYD touchpad detected\n");
455
456 if (set_properties) {
457 psmouse->vendor = "BYD";
458 psmouse->name = "TouchPad";
459 }
460
461 return 0;
462}
463
464int byd_init(struct psmouse *psmouse)
465{
466 struct input_dev *dev = psmouse->dev;
467 struct byd_data *priv;
468
469 if (psmouse_reset(psmouse))
470 return -EIO;
471
472 if (byd_reset_touchpad(psmouse))
473 return -EIO;
474
475 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
476 if (!priv)
477 return -ENOMEM;
478
479 priv->psmouse = psmouse;
480 timer_setup(&priv->timer, byd_clear_touch, 0);
481
482 psmouse->private = priv;
483 psmouse->disconnect = byd_disconnect;
484 psmouse->reconnect = byd_reconnect;
485 psmouse->protocol_handler = byd_process_byte;
486 psmouse->pktsize = 4;
487 psmouse->resync_time = 0;
488
489 __set_bit(INPUT_PROP_POINTER, dev->propbit);
490 /* Touchpad */
491 __set_bit(BTN_TOUCH, dev->keybit);
492 __set_bit(BTN_TOOL_FINGER, dev->keybit);
493 /* Buttons */
494 __set_bit(BTN_LEFT, dev->keybit);
495 __set_bit(BTN_RIGHT, dev->keybit);
496 __clear_bit(BTN_MIDDLE, dev->keybit);
497
498 /* Absolute position */
499 __set_bit(EV_ABS, dev->evbit);
500 input_set_abs_params(dev, ABS_X, 0, BYD_PAD_WIDTH, 0, 0);
501 input_set_abs_params(dev, ABS_Y, 0, BYD_PAD_HEIGHT, 0, 0);
502 input_abs_set_res(dev, ABS_X, BYD_PAD_RESOLUTION);
503 input_abs_set_res(dev, ABS_Y, BYD_PAD_RESOLUTION);
504 /* No relative support */
505 __clear_bit(EV_REL, dev->evbit);
506 __clear_bit(REL_X, dev->relbit);
507 __clear_bit(REL_Y, dev->relbit);
508
509 return 0;
510}
1/*
2 * BYD TouchPad PS/2 mouse driver
3 *
4 * Copyright (C) 2015 Chris Diamand <chris@diamand.org>
5 * Copyright (C) 2015 Richard Pospesel
6 * Copyright (C) 2015 Tai Chi Minh Ralph Eastwood
7 * Copyright (C) 2015 Martin Wimpress
8 * Copyright (C) 2015 Jay Kuri
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 */
14
15#include <linux/delay.h>
16#include <linux/input.h>
17#include <linux/libps2.h>
18#include <linux/serio.h>
19#include <linux/slab.h>
20
21#include "psmouse.h"
22#include "byd.h"
23
24/* PS2 Bits */
25#define PS2_Y_OVERFLOW BIT_MASK(7)
26#define PS2_X_OVERFLOW BIT_MASK(6)
27#define PS2_Y_SIGN BIT_MASK(5)
28#define PS2_X_SIGN BIT_MASK(4)
29#define PS2_ALWAYS_1 BIT_MASK(3)
30#define PS2_MIDDLE BIT_MASK(2)
31#define PS2_RIGHT BIT_MASK(1)
32#define PS2_LEFT BIT_MASK(0)
33
34/*
35 * BYD pad constants
36 */
37
38/*
39 * True device resolution is unknown, however experiments show the
40 * resolution is about 111 units/mm.
41 * Absolute coordinate packets are in the range 0-255 for both X and Y
42 * we pick ABS_X/ABS_Y dimensions which are multiples of 256 and in
43 * the right ballpark given the touchpad's physical dimensions and estimate
44 * resolution per spec sheet, device active area dimensions are
45 * 101.6 x 60.1 mm.
46 */
47#define BYD_PAD_WIDTH 11264
48#define BYD_PAD_HEIGHT 6656
49#define BYD_PAD_RESOLUTION 111
50
51/*
52 * Given the above dimensions, relative packets velocity is in multiples of
53 * 1 unit / 11 milliseconds. We use this dt to estimate distance traveled
54 */
55#define BYD_DT 11
56/* Time in jiffies used to timeout various touch events (64 ms) */
57#define BYD_TOUCH_TIMEOUT msecs_to_jiffies(64)
58
59/* BYD commands reverse engineered from windows driver */
60
61/*
62 * Swipe gesture from off-pad to on-pad
63 * 0 : disable
64 * 1 : enable
65 */
66#define BYD_CMD_SET_OFFSCREEN_SWIPE 0x10cc
67/*
68 * Tap and drag delay time
69 * 0 : disable
70 * 1 - 8 : least to most delay
71 */
72#define BYD_CMD_SET_TAP_DRAG_DELAY_TIME 0x10cf
73/*
74 * Physical buttons function mapping
75 * 0 : enable
76 * 4 : normal
77 * 5 : left button custom command
78 * 6 : right button custom command
79 * 8 : disable
80 */
81#define BYD_CMD_SET_PHYSICAL_BUTTONS 0x10d0
82/*
83 * Absolute mode (1 byte X/Y resolution)
84 * 0 : disable
85 * 2 : enable
86 */
87#define BYD_CMD_SET_ABSOLUTE_MODE 0x10d1
88/*
89 * Two finger scrolling
90 * 1 : vertical
91 * 2 : horizontal
92 * 3 : vertical + horizontal
93 * 4 : disable
94 */
95#define BYD_CMD_SET_TWO_FINGER_SCROLL 0x10d2
96/*
97 * Handedness
98 * 1 : right handed
99 * 2 : left handed
100 */
101#define BYD_CMD_SET_HANDEDNESS 0x10d3
102/*
103 * Tap to click
104 * 1 : enable
105 * 2 : disable
106 */
107#define BYD_CMD_SET_TAP 0x10d4
108/*
109 * Tap and drag
110 * 1 : tap and hold to drag
111 * 2 : tap and hold to drag + lock
112 * 3 : disable
113 */
114#define BYD_CMD_SET_TAP_DRAG 0x10d5
115/*
116 * Touch sensitivity
117 * 1 - 7 : least to most sensitive
118 */
119#define BYD_CMD_SET_TOUCH_SENSITIVITY 0x10d6
120/*
121 * One finger scrolling
122 * 1 : vertical
123 * 2 : horizontal
124 * 3 : vertical + horizontal
125 * 4 : disable
126 */
127#define BYD_CMD_SET_ONE_FINGER_SCROLL 0x10d7
128/*
129 * One finger scrolling function
130 * 1 : free scrolling
131 * 2 : edge motion
132 * 3 : free scrolling + edge motion
133 * 4 : disable
134 */
135#define BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC 0x10d8
136/*
137 * Sliding speed
138 * 1 - 5 : slowest to fastest
139 */
140#define BYD_CMD_SET_SLIDING_SPEED 0x10da
141/*
142 * Edge motion
143 * 1 : disable
144 * 2 : enable when dragging
145 * 3 : enable when dragging and pointing
146 */
147#define BYD_CMD_SET_EDGE_MOTION 0x10db
148/*
149 * Left edge region size
150 * 0 - 7 : smallest to largest width
151 */
152#define BYD_CMD_SET_LEFT_EDGE_REGION 0x10dc
153/*
154 * Top edge region size
155 * 0 - 9 : smallest to largest height
156 */
157#define BYD_CMD_SET_TOP_EDGE_REGION 0x10dd
158/*
159 * Disregard palm press as clicks
160 * 1 - 6 : smallest to largest
161 */
162#define BYD_CMD_SET_PALM_CHECK 0x10de
163/*
164 * Right edge region size
165 * 0 - 7 : smallest to largest width
166 */
167#define BYD_CMD_SET_RIGHT_EDGE_REGION 0x10df
168/*
169 * Bottom edge region size
170 * 0 - 9 : smallest to largest height
171 */
172#define BYD_CMD_SET_BOTTOM_EDGE_REGION 0x10e1
173/*
174 * Multitouch gestures
175 * 1 : enable
176 * 2 : disable
177 */
178#define BYD_CMD_SET_MULTITOUCH 0x10e3
179/*
180 * Edge motion speed
181 * 0 : control with finger pressure
182 * 1 - 9 : slowest to fastest
183 */
184#define BYD_CMD_SET_EDGE_MOTION_SPEED 0x10e4
185/*
186 * Two finger scolling function
187 * 0 : free scrolling
188 * 1 : free scrolling (with momentum)
189 * 2 : edge motion
190 * 3 : free scrolling (with momentum) + edge motion
191 * 4 : disable
192 */
193#define BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC 0x10e5
194
195/*
196 * The touchpad generates a mixture of absolute and relative packets, indicated
197 * by the the last byte of each packet being set to one of the following:
198 */
199#define BYD_PACKET_ABSOLUTE 0xf8
200#define BYD_PACKET_RELATIVE 0x00
201/* Multitouch gesture packets */
202#define BYD_PACKET_PINCH_IN 0xd8
203#define BYD_PACKET_PINCH_OUT 0x28
204#define BYD_PACKET_ROTATE_CLOCKWISE 0x29
205#define BYD_PACKET_ROTATE_ANTICLOCKWISE 0xd7
206#define BYD_PACKET_TWO_FINGER_SCROLL_RIGHT 0x2a
207#define BYD_PACKET_TWO_FINGER_SCROLL_DOWN 0x2b
208#define BYD_PACKET_TWO_FINGER_SCROLL_UP 0xd5
209#define BYD_PACKET_TWO_FINGER_SCROLL_LEFT 0xd6
210#define BYD_PACKET_THREE_FINGER_SWIPE_RIGHT 0x2c
211#define BYD_PACKET_THREE_FINGER_SWIPE_DOWN 0x2d
212#define BYD_PACKET_THREE_FINGER_SWIPE_UP 0xd3
213#define BYD_PACKET_THREE_FINGER_SWIPE_LEFT 0xd4
214#define BYD_PACKET_FOUR_FINGER_DOWN 0x33
215#define BYD_PACKET_FOUR_FINGER_UP 0xcd
216#define BYD_PACKET_REGION_SCROLL_RIGHT 0x35
217#define BYD_PACKET_REGION_SCROLL_DOWN 0x36
218#define BYD_PACKET_REGION_SCROLL_UP 0xca
219#define BYD_PACKET_REGION_SCROLL_LEFT 0xcb
220#define BYD_PACKET_RIGHT_CORNER_CLICK 0xd2
221#define BYD_PACKET_LEFT_CORNER_CLICK 0x2e
222#define BYD_PACKET_LEFT_AND_RIGHT_CORNER_CLICK 0x2f
223#define BYD_PACKET_ONTO_PAD_SWIPE_RIGHT 0x37
224#define BYD_PACKET_ONTO_PAD_SWIPE_DOWN 0x30
225#define BYD_PACKET_ONTO_PAD_SWIPE_UP 0xd0
226#define BYD_PACKET_ONTO_PAD_SWIPE_LEFT 0xc9
227
228struct byd_data {
229 struct timer_list timer;
230 s32 abs_x;
231 s32 abs_y;
232 typeof(jiffies) last_touch_time;
233 bool btn_left;
234 bool btn_right;
235 bool touch;
236};
237
238static void byd_report_input(struct psmouse *psmouse)
239{
240 struct byd_data *priv = psmouse->private;
241 struct input_dev *dev = psmouse->dev;
242
243 input_report_key(dev, BTN_TOUCH, priv->touch);
244 input_report_key(dev, BTN_TOOL_FINGER, priv->touch);
245
246 input_report_abs(dev, ABS_X, priv->abs_x);
247 input_report_abs(dev, ABS_Y, priv->abs_y);
248 input_report_key(dev, BTN_LEFT, priv->btn_left);
249 input_report_key(dev, BTN_RIGHT, priv->btn_right);
250
251 input_sync(dev);
252}
253
254static void byd_clear_touch(unsigned long data)
255{
256 struct psmouse *psmouse = (struct psmouse *)data;
257 struct byd_data *priv = psmouse->private;
258
259 serio_pause_rx(psmouse->ps2dev.serio);
260 priv->touch = false;
261
262 byd_report_input(psmouse);
263
264 serio_continue_rx(psmouse->ps2dev.serio);
265
266 /*
267 * Move cursor back to center of pad when we lose touch - this
268 * specifically improves user experience when moving cursor with one
269 * finger, and pressing a button with another.
270 */
271 priv->abs_x = BYD_PAD_WIDTH / 2;
272 priv->abs_y = BYD_PAD_HEIGHT / 2;
273}
274
275static psmouse_ret_t byd_process_byte(struct psmouse *psmouse)
276{
277 struct byd_data *priv = psmouse->private;
278 u8 *pkt = psmouse->packet;
279
280 if (psmouse->pktcnt > 0 && !(pkt[0] & PS2_ALWAYS_1)) {
281 psmouse_warn(psmouse, "Always_1 bit not 1. pkt[0] = %02x\n",
282 pkt[0]);
283 return PSMOUSE_BAD_DATA;
284 }
285
286 if (psmouse->pktcnt < psmouse->pktsize)
287 return PSMOUSE_GOOD_DATA;
288
289 /* Otherwise, a full packet has been received */
290 switch (pkt[3]) {
291 case BYD_PACKET_ABSOLUTE:
292 /* Only use absolute packets for the start of movement. */
293 if (!priv->touch) {
294 /* needed to detect tap */
295 typeof(jiffies) tap_time =
296 priv->last_touch_time + BYD_TOUCH_TIMEOUT;
297 priv->touch = time_after(jiffies, tap_time);
298
299 /* init abs position */
300 priv->abs_x = pkt[1] * (BYD_PAD_WIDTH / 256);
301 priv->abs_y = (255 - pkt[2]) * (BYD_PAD_HEIGHT / 256);
302 }
303 break;
304 case BYD_PACKET_RELATIVE: {
305 /* Standard packet */
306 /* Sign-extend if a sign bit is set. */
307 u32 signx = pkt[0] & PS2_X_SIGN ? ~0xFF : 0;
308 u32 signy = pkt[0] & PS2_Y_SIGN ? ~0xFF : 0;
309 s32 dx = signx | (int) pkt[1];
310 s32 dy = signy | (int) pkt[2];
311
312 /* Update position based on velocity */
313 priv->abs_x += dx * BYD_DT;
314 priv->abs_y -= dy * BYD_DT;
315
316 priv->touch = true;
317 break;
318 }
319 default:
320 psmouse_warn(psmouse,
321 "Unrecognized Z: pkt = %02x %02x %02x %02x\n",
322 psmouse->packet[0], psmouse->packet[1],
323 psmouse->packet[2], psmouse->packet[3]);
324 return PSMOUSE_BAD_DATA;
325 }
326
327 priv->btn_left = pkt[0] & PS2_LEFT;
328 priv->btn_right = pkt[0] & PS2_RIGHT;
329
330 byd_report_input(psmouse);
331
332 /* Reset time since last touch. */
333 if (priv->touch) {
334 priv->last_touch_time = jiffies;
335 mod_timer(&priv->timer, jiffies + BYD_TOUCH_TIMEOUT);
336 }
337
338 return PSMOUSE_FULL_PACKET;
339}
340
341static int byd_reset_touchpad(struct psmouse *psmouse)
342{
343 struct ps2dev *ps2dev = &psmouse->ps2dev;
344 u8 param[4];
345 size_t i;
346
347 const struct {
348 u16 command;
349 u8 arg;
350 } seq[] = {
351 /*
352 * Intellimouse initialization sequence, to get 4-byte instead
353 * of 3-byte packets.
354 */
355 { PSMOUSE_CMD_SETRATE, 0xC8 },
356 { PSMOUSE_CMD_SETRATE, 0x64 },
357 { PSMOUSE_CMD_SETRATE, 0x50 },
358 { PSMOUSE_CMD_GETID, 0 },
359 { PSMOUSE_CMD_ENABLE, 0 },
360 /*
361 * BYD-specific initialization, which enables absolute mode and
362 * (if desired), the touchpad's built-in gesture detection.
363 */
364 { 0x10E2, 0x00 },
365 { 0x10E0, 0x02 },
366 /* The touchpad should reply with 4 seemingly-random bytes */
367 { 0x14E0, 0x01 },
368 /* Pairs of parameters and values. */
369 { BYD_CMD_SET_HANDEDNESS, 0x01 },
370 { BYD_CMD_SET_PHYSICAL_BUTTONS, 0x04 },
371 { BYD_CMD_SET_TAP, 0x02 },
372 { BYD_CMD_SET_ONE_FINGER_SCROLL, 0x04 },
373 { BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC, 0x04 },
374 { BYD_CMD_SET_EDGE_MOTION, 0x01 },
375 { BYD_CMD_SET_PALM_CHECK, 0x00 },
376 { BYD_CMD_SET_MULTITOUCH, 0x02 },
377 { BYD_CMD_SET_TWO_FINGER_SCROLL, 0x04 },
378 { BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC, 0x04 },
379 { BYD_CMD_SET_LEFT_EDGE_REGION, 0x00 },
380 { BYD_CMD_SET_TOP_EDGE_REGION, 0x00 },
381 { BYD_CMD_SET_RIGHT_EDGE_REGION, 0x00 },
382 { BYD_CMD_SET_BOTTOM_EDGE_REGION, 0x00 },
383 { BYD_CMD_SET_ABSOLUTE_MODE, 0x02 },
384 /* Finalize initialization. */
385 { 0x10E0, 0x00 },
386 { 0x10E2, 0x01 },
387 };
388
389 for (i = 0; i < ARRAY_SIZE(seq); ++i) {
390 memset(param, 0, sizeof(param));
391 param[0] = seq[i].arg;
392 if (ps2_command(ps2dev, param, seq[i].command))
393 return -EIO;
394 }
395
396 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
397 return 0;
398}
399
400static int byd_reconnect(struct psmouse *psmouse)
401{
402 int retry = 0, error = 0;
403
404 psmouse_dbg(psmouse, "Reconnect\n");
405 do {
406 psmouse_reset(psmouse);
407 if (retry)
408 ssleep(1);
409 error = byd_detect(psmouse, 0);
410 } while (error && ++retry < 3);
411
412 if (error)
413 return error;
414
415 psmouse_dbg(psmouse, "Reconnected after %d attempts\n", retry);
416
417 error = byd_reset_touchpad(psmouse);
418 if (error) {
419 psmouse_err(psmouse, "Unable to initialize device\n");
420 return error;
421 }
422
423 return 0;
424}
425
426static void byd_disconnect(struct psmouse *psmouse)
427{
428 struct byd_data *priv = psmouse->private;
429
430 if (priv) {
431 del_timer(&priv->timer);
432 kfree(psmouse->private);
433 psmouse->private = NULL;
434 }
435}
436
437int byd_detect(struct psmouse *psmouse, bool set_properties)
438{
439 struct ps2dev *ps2dev = &psmouse->ps2dev;
440 u8 param[4] = {0x03, 0x00, 0x00, 0x00};
441
442 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
443 return -1;
444 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
445 return -1;
446 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
447 return -1;
448 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
449 return -1;
450 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
451 return -1;
452
453 if (param[1] != 0x03 || param[2] != 0x64)
454 return -ENODEV;
455
456 psmouse_dbg(psmouse, "BYD touchpad detected\n");
457
458 if (set_properties) {
459 psmouse->vendor = "BYD";
460 psmouse->name = "TouchPad";
461 }
462
463 return 0;
464}
465
466int byd_init(struct psmouse *psmouse)
467{
468 struct input_dev *dev = psmouse->dev;
469 struct byd_data *priv;
470
471 if (psmouse_reset(psmouse))
472 return -EIO;
473
474 if (byd_reset_touchpad(psmouse))
475 return -EIO;
476
477 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
478 if (!priv)
479 return -ENOMEM;
480
481 memset(priv, 0, sizeof(*priv));
482 setup_timer(&priv->timer, byd_clear_touch, (unsigned long) psmouse);
483
484 psmouse->private = priv;
485 psmouse->disconnect = byd_disconnect;
486 psmouse->reconnect = byd_reconnect;
487 psmouse->protocol_handler = byd_process_byte;
488 psmouse->pktsize = 4;
489 psmouse->resync_time = 0;
490
491 __set_bit(INPUT_PROP_POINTER, dev->propbit);
492 /* Touchpad */
493 __set_bit(BTN_TOUCH, dev->keybit);
494 __set_bit(BTN_TOOL_FINGER, dev->keybit);
495 /* Buttons */
496 __set_bit(BTN_LEFT, dev->keybit);
497 __set_bit(BTN_RIGHT, dev->keybit);
498 __clear_bit(BTN_MIDDLE, dev->keybit);
499
500 /* Absolute position */
501 __set_bit(EV_ABS, dev->evbit);
502 input_set_abs_params(dev, ABS_X, 0, BYD_PAD_WIDTH, 0, 0);
503 input_set_abs_params(dev, ABS_Y, 0, BYD_PAD_HEIGHT, 0, 0);
504 input_abs_set_res(dev, ABS_X, BYD_PAD_RESOLUTION);
505 input_abs_set_res(dev, ABS_Y, BYD_PAD_RESOLUTION);
506 /* No relative support */
507 __clear_bit(EV_REL, dev->evbit);
508 __clear_bit(REL_X, dev->relbit);
509 __clear_bit(REL_Y, dev->relbit);
510
511 return 0;
512}