1 // SPDX-License-Identifier: GPL-2.0-only
3 * BYD TouchPad PS/2 mouse driver
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
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>
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)
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
44 #define BYD_PAD_WIDTH 11264
45 #define BYD_PAD_HEIGHT 6656
46 #define BYD_PAD_RESOLUTION 111
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
53 /* Time in jiffies used to timeout various touch events (64 ms) */
54 #define BYD_TOUCH_TIMEOUT msecs_to_jiffies(64)
56 /* BYD commands reverse engineered from windows driver */
59 * Swipe gesture from off-pad to on-pad
63 #define BYD_CMD_SET_OFFSCREEN_SWIPE 0x10cc
65 * Tap and drag delay time
67 * 1 - 8 : least to most delay
69 #define BYD_CMD_SET_TAP_DRAG_DELAY_TIME 0x10cf
71 * Physical buttons function mapping
74 * 5 : left button custom command
75 * 6 : right button custom command
78 #define BYD_CMD_SET_PHYSICAL_BUTTONS 0x10d0
80 * Absolute mode (1 byte X/Y resolution)
84 #define BYD_CMD_SET_ABSOLUTE_MODE 0x10d1
86 * Two finger scrolling
89 * 3 : vertical + horizontal
92 #define BYD_CMD_SET_TWO_FINGER_SCROLL 0x10d2
98 #define BYD_CMD_SET_HANDEDNESS 0x10d3
104 #define BYD_CMD_SET_TAP 0x10d4
107 * 1 : tap and hold to drag
108 * 2 : tap and hold to drag + lock
111 #define BYD_CMD_SET_TAP_DRAG 0x10d5
114 * 1 - 7 : least to most sensitive
116 #define BYD_CMD_SET_TOUCH_SENSITIVITY 0x10d6
118 * One finger scrolling
121 * 3 : vertical + horizontal
124 #define BYD_CMD_SET_ONE_FINGER_SCROLL 0x10d7
126 * One finger scrolling function
129 * 3 : free scrolling + edge motion
132 #define BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC 0x10d8
135 * 1 - 5 : slowest to fastest
137 #define BYD_CMD_SET_SLIDING_SPEED 0x10da
141 * 2 : enable when dragging
142 * 3 : enable when dragging and pointing
144 #define BYD_CMD_SET_EDGE_MOTION 0x10db
146 * Left edge region size
147 * 0 - 7 : smallest to largest width
149 #define BYD_CMD_SET_LEFT_EDGE_REGION 0x10dc
151 * Top edge region size
152 * 0 - 9 : smallest to largest height
154 #define BYD_CMD_SET_TOP_EDGE_REGION 0x10dd
156 * Disregard palm press as clicks
157 * 1 - 6 : smallest to largest
159 #define BYD_CMD_SET_PALM_CHECK 0x10de
161 * Right edge region size
162 * 0 - 7 : smallest to largest width
164 #define BYD_CMD_SET_RIGHT_EDGE_REGION 0x10df
166 * Bottom edge region size
167 * 0 - 9 : smallest to largest height
169 #define BYD_CMD_SET_BOTTOM_EDGE_REGION 0x10e1
171 * Multitouch gestures
175 #define BYD_CMD_SET_MULTITOUCH 0x10e3
178 * 0 : control with finger pressure
179 * 1 - 9 : slowest to fastest
181 #define BYD_CMD_SET_EDGE_MOTION_SPEED 0x10e4
183 * Two finger scolling function
185 * 1 : free scrolling (with momentum)
187 * 3 : free scrolling (with momentum) + edge motion
190 #define BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC 0x10e5
193 * The touchpad generates a mixture of absolute and relative packets, indicated
194 * by the the last byte of each packet being set to one of the following:
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
226 struct timer_list timer
;
227 struct psmouse
*psmouse
;
230 typeof(jiffies
) last_touch_time
;
236 static void byd_report_input(struct psmouse
*psmouse
)
238 struct byd_data
*priv
= psmouse
->private;
239 struct input_dev
*dev
= psmouse
->dev
;
241 input_report_key(dev
, BTN_TOUCH
, priv
->touch
);
242 input_report_key(dev
, BTN_TOOL_FINGER
, priv
->touch
);
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
);
252 static void byd_clear_touch(struct timer_list
*t
)
254 struct byd_data
*priv
= from_timer(priv
, t
, timer
);
255 struct psmouse
*psmouse
= priv
->psmouse
;
257 serio_pause_rx(psmouse
->ps2dev
.serio
);
260 byd_report_input(psmouse
);
262 serio_continue_rx(psmouse
->ps2dev
.serio
);
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.
269 priv
->abs_x
= BYD_PAD_WIDTH
/ 2;
270 priv
->abs_y
= BYD_PAD_HEIGHT
/ 2;
273 static psmouse_ret_t
byd_process_byte(struct psmouse
*psmouse
)
275 struct byd_data
*priv
= psmouse
->private;
276 u8
*pkt
= psmouse
->packet
;
278 if (psmouse
->pktcnt
> 0 && !(pkt
[0] & PS2_ALWAYS_1
)) {
279 psmouse_warn(psmouse
, "Always_1 bit not 1. pkt[0] = %02x\n",
281 return PSMOUSE_BAD_DATA
;
284 if (psmouse
->pktcnt
< psmouse
->pktsize
)
285 return PSMOUSE_GOOD_DATA
;
287 /* Otherwise, a full packet has been received */
289 case BYD_PACKET_ABSOLUTE
:
290 /* Only use absolute packets for the start of movement. */
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
);
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);
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];
310 /* Update position based on velocity */
311 priv
->abs_x
+= dx
* BYD_DT
;
312 priv
->abs_y
-= dy
* BYD_DT
;
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
;
325 priv
->btn_left
= pkt
[0] & PS2_LEFT
;
326 priv
->btn_right
= pkt
[0] & PS2_RIGHT
;
328 byd_report_input(psmouse
);
330 /* Reset time since last touch. */
332 priv
->last_touch_time
= jiffies
;
333 mod_timer(&priv
->timer
, jiffies
+ BYD_TOUCH_TIMEOUT
);
336 return PSMOUSE_FULL_PACKET
;
339 static int byd_reset_touchpad(struct psmouse
*psmouse
)
341 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
345 static const struct {
350 * Intellimouse initialization sequence, to get 4-byte instead
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 },
359 * BYD-specific initialization, which enables absolute mode and
360 * (if desired), the touchpad's built-in gesture detection.
364 /* The touchpad should reply with 4 seemingly-random bytes */
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. */
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
))
394 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
398 static int byd_reconnect(struct psmouse
*psmouse
)
400 int retry
= 0, error
= 0;
402 psmouse_dbg(psmouse
, "Reconnect\n");
404 psmouse_reset(psmouse
);
407 error
= byd_detect(psmouse
, 0);
408 } while (error
&& ++retry
< 3);
413 psmouse_dbg(psmouse
, "Reconnected after %d attempts\n", retry
);
415 error
= byd_reset_touchpad(psmouse
);
417 psmouse_err(psmouse
, "Unable to initialize device\n");
424 static void byd_disconnect(struct psmouse
*psmouse
)
426 struct byd_data
*priv
= psmouse
->private;
429 del_timer(&priv
->timer
);
430 kfree(psmouse
->private);
431 psmouse
->private = NULL
;
435 int byd_detect(struct psmouse
*psmouse
, bool set_properties
)
437 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
438 u8 param
[4] = {0x03, 0x00, 0x00, 0x00};
440 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
))
442 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
))
444 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
))
446 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
))
448 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
))
451 if (param
[1] != 0x03 || param
[2] != 0x64)
454 psmouse_dbg(psmouse
, "BYD touchpad detected\n");
456 if (set_properties
) {
457 psmouse
->vendor
= "BYD";
458 psmouse
->name
= "TouchPad";
464 int byd_init(struct psmouse
*psmouse
)
466 struct input_dev
*dev
= psmouse
->dev
;
467 struct byd_data
*priv
;
469 if (psmouse_reset(psmouse
))
472 if (byd_reset_touchpad(psmouse
))
475 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
479 priv
->psmouse
= psmouse
;
480 timer_setup(&priv
->timer
, byd_clear_touch
, 0);
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;
489 __set_bit(INPUT_PROP_POINTER
, dev
->propbit
);
491 __set_bit(BTN_TOUCH
, dev
->keybit
);
492 __set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
494 __set_bit(BTN_LEFT
, dev
->keybit
);
495 __set_bit(BTN_RIGHT
, dev
->keybit
);
496 __clear_bit(BTN_MIDDLE
, dev
->keybit
);
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
);