xref: /linux/drivers/input/mouse/byd.c (revision fd639726bf15fca8ee1a00dce8e0096d0ad9bd18)
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 
228 struct byd_data {
229 	struct timer_list timer;
230 	struct psmouse *psmouse;
231 	s32 abs_x;
232 	s32 abs_y;
233 	typeof(jiffies) last_touch_time;
234 	bool btn_left;
235 	bool btn_right;
236 	bool touch;
237 };
238 
239 static void byd_report_input(struct psmouse *psmouse)
240 {
241 	struct byd_data *priv = psmouse->private;
242 	struct input_dev *dev = psmouse->dev;
243 
244 	input_report_key(dev, BTN_TOUCH, priv->touch);
245 	input_report_key(dev, BTN_TOOL_FINGER, priv->touch);
246 
247 	input_report_abs(dev, ABS_X, priv->abs_x);
248 	input_report_abs(dev, ABS_Y, priv->abs_y);
249 	input_report_key(dev, BTN_LEFT, priv->btn_left);
250 	input_report_key(dev, BTN_RIGHT, priv->btn_right);
251 
252 	input_sync(dev);
253 }
254 
255 static void byd_clear_touch(struct timer_list *t)
256 {
257 	struct byd_data *priv = from_timer(priv, t, timer);
258 	struct psmouse *psmouse = priv->psmouse;
259 
260 	serio_pause_rx(psmouse->ps2dev.serio);
261 	priv->touch = false;
262 
263 	byd_report_input(psmouse);
264 
265 	serio_continue_rx(psmouse->ps2dev.serio);
266 
267 	/*
268 	 * Move cursor back to center of pad when we lose touch - this
269 	 * specifically improves user experience when moving cursor with one
270 	 * finger, and pressing a button with another.
271 	 */
272 	priv->abs_x = BYD_PAD_WIDTH / 2;
273 	priv->abs_y = BYD_PAD_HEIGHT / 2;
274 }
275 
276 static psmouse_ret_t byd_process_byte(struct psmouse *psmouse)
277 {
278 	struct byd_data *priv = psmouse->private;
279 	u8 *pkt = psmouse->packet;
280 
281 	if (psmouse->pktcnt > 0 && !(pkt[0] & PS2_ALWAYS_1)) {
282 		psmouse_warn(psmouse, "Always_1 bit not 1. pkt[0] = %02x\n",
283 			     pkt[0]);
284 		return PSMOUSE_BAD_DATA;
285 	}
286 
287 	if (psmouse->pktcnt < psmouse->pktsize)
288 		return PSMOUSE_GOOD_DATA;
289 
290 	/* Otherwise, a full packet has been received */
291 	switch (pkt[3]) {
292 	case BYD_PACKET_ABSOLUTE:
293 		/* Only use absolute packets for the start of movement. */
294 		if (!priv->touch) {
295 			/* needed to detect tap */
296 			typeof(jiffies) tap_time =
297 				priv->last_touch_time + BYD_TOUCH_TIMEOUT;
298 			priv->touch = time_after(jiffies, tap_time);
299 
300 			/* init abs position */
301 			priv->abs_x = pkt[1] * (BYD_PAD_WIDTH / 256);
302 			priv->abs_y = (255 - pkt[2]) * (BYD_PAD_HEIGHT / 256);
303 		}
304 		break;
305 	case BYD_PACKET_RELATIVE: {
306 		/* Standard packet */
307 		/* Sign-extend if a sign bit is set. */
308 		u32 signx = pkt[0] & PS2_X_SIGN ? ~0xFF : 0;
309 		u32 signy = pkt[0] & PS2_Y_SIGN ? ~0xFF : 0;
310 		s32 dx = signx | (int) pkt[1];
311 		s32 dy = signy | (int) pkt[2];
312 
313 		/* Update position based on velocity */
314 		priv->abs_x += dx * BYD_DT;
315 		priv->abs_y -= dy * BYD_DT;
316 
317 		priv->touch = true;
318 		break;
319 	}
320 	default:
321 		psmouse_warn(psmouse,
322 			     "Unrecognized Z: pkt = %02x %02x %02x %02x\n",
323 			     psmouse->packet[0], psmouse->packet[1],
324 			     psmouse->packet[2], psmouse->packet[3]);
325 		return PSMOUSE_BAD_DATA;
326 	}
327 
328 	priv->btn_left = pkt[0] & PS2_LEFT;
329 	priv->btn_right = pkt[0] & PS2_RIGHT;
330 
331 	byd_report_input(psmouse);
332 
333 	/* Reset time since last touch. */
334 	if (priv->touch) {
335 		priv->last_touch_time = jiffies;
336 		mod_timer(&priv->timer, jiffies + BYD_TOUCH_TIMEOUT);
337 	}
338 
339 	return PSMOUSE_FULL_PACKET;
340 }
341 
342 static int byd_reset_touchpad(struct psmouse *psmouse)
343 {
344 	struct ps2dev *ps2dev = &psmouse->ps2dev;
345 	u8 param[4];
346 	size_t i;
347 
348 	static const struct {
349 		u16 command;
350 		u8 arg;
351 	} seq[] = {
352 		/*
353 		 * Intellimouse initialization sequence, to get 4-byte instead
354 		 * of 3-byte packets.
355 		 */
356 		{ PSMOUSE_CMD_SETRATE, 0xC8 },
357 		{ PSMOUSE_CMD_SETRATE, 0x64 },
358 		{ PSMOUSE_CMD_SETRATE, 0x50 },
359 		{ PSMOUSE_CMD_GETID, 0 },
360 		{ PSMOUSE_CMD_ENABLE, 0 },
361 		/*
362 		 * BYD-specific initialization, which enables absolute mode and
363 		 * (if desired), the touchpad's built-in gesture detection.
364 		 */
365 		{ 0x10E2, 0x00 },
366 		{ 0x10E0, 0x02 },
367 		/* The touchpad should reply with 4 seemingly-random bytes */
368 		{ 0x14E0, 0x01 },
369 		/* Pairs of parameters and values. */
370 		{ BYD_CMD_SET_HANDEDNESS, 0x01 },
371 		{ BYD_CMD_SET_PHYSICAL_BUTTONS, 0x04 },
372 		{ BYD_CMD_SET_TAP, 0x02 },
373 		{ BYD_CMD_SET_ONE_FINGER_SCROLL, 0x04 },
374 		{ BYD_CMD_SET_ONE_FINGER_SCROLL_FUNC, 0x04 },
375 		{ BYD_CMD_SET_EDGE_MOTION, 0x01 },
376 		{ BYD_CMD_SET_PALM_CHECK, 0x00 },
377 		{ BYD_CMD_SET_MULTITOUCH, 0x02 },
378 		{ BYD_CMD_SET_TWO_FINGER_SCROLL, 0x04 },
379 		{ BYD_CMD_SET_TWO_FINGER_SCROLL_FUNC, 0x04 },
380 		{ BYD_CMD_SET_LEFT_EDGE_REGION, 0x00 },
381 		{ BYD_CMD_SET_TOP_EDGE_REGION, 0x00 },
382 		{ BYD_CMD_SET_RIGHT_EDGE_REGION, 0x00 },
383 		{ BYD_CMD_SET_BOTTOM_EDGE_REGION, 0x00 },
384 		{ BYD_CMD_SET_ABSOLUTE_MODE, 0x02 },
385 		/* Finalize initialization. */
386 		{ 0x10E0, 0x00 },
387 		{ 0x10E2, 0x01 },
388 	};
389 
390 	for (i = 0; i < ARRAY_SIZE(seq); ++i) {
391 		memset(param, 0, sizeof(param));
392 		param[0] = seq[i].arg;
393 		if (ps2_command(ps2dev, param, seq[i].command))
394 			return -EIO;
395 	}
396 
397 	psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
398 	return 0;
399 }
400 
401 static int byd_reconnect(struct psmouse *psmouse)
402 {
403 	int retry = 0, error = 0;
404 
405 	psmouse_dbg(psmouse, "Reconnect\n");
406 	do {
407 		psmouse_reset(psmouse);
408 		if (retry)
409 			ssleep(1);
410 		error = byd_detect(psmouse, 0);
411 	} while (error && ++retry < 3);
412 
413 	if (error)
414 		return error;
415 
416 	psmouse_dbg(psmouse, "Reconnected after %d attempts\n", retry);
417 
418 	error = byd_reset_touchpad(psmouse);
419 	if (error) {
420 		psmouse_err(psmouse, "Unable to initialize device\n");
421 		return error;
422 	}
423 
424 	return 0;
425 }
426 
427 static void byd_disconnect(struct psmouse *psmouse)
428 {
429 	struct byd_data *priv = psmouse->private;
430 
431 	if (priv) {
432 		del_timer(&priv->timer);
433 		kfree(psmouse->private);
434 		psmouse->private = NULL;
435 	}
436 }
437 
438 int byd_detect(struct psmouse *psmouse, bool set_properties)
439 {
440 	struct ps2dev *ps2dev = &psmouse->ps2dev;
441 	u8 param[4] = {0x03, 0x00, 0x00, 0x00};
442 
443 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
444 		return -1;
445 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
446 		return -1;
447 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
448 		return -1;
449 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
450 		return -1;
451 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
452 		return -1;
453 
454 	if (param[1] != 0x03 || param[2] != 0x64)
455 		return -ENODEV;
456 
457 	psmouse_dbg(psmouse, "BYD touchpad detected\n");
458 
459 	if (set_properties) {
460 		psmouse->vendor = "BYD";
461 		psmouse->name = "TouchPad";
462 	}
463 
464 	return 0;
465 }
466 
467 int byd_init(struct psmouse *psmouse)
468 {
469 	struct input_dev *dev = psmouse->dev;
470 	struct byd_data *priv;
471 
472 	if (psmouse_reset(psmouse))
473 		return -EIO;
474 
475 	if (byd_reset_touchpad(psmouse))
476 		return -EIO;
477 
478 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
479 	if (!priv)
480 		return -ENOMEM;
481 
482 	priv->psmouse = psmouse;
483 	timer_setup(&priv->timer, byd_clear_touch, 0);
484 
485 	psmouse->private = priv;
486 	psmouse->disconnect = byd_disconnect;
487 	psmouse->reconnect = byd_reconnect;
488 	psmouse->protocol_handler = byd_process_byte;
489 	psmouse->pktsize = 4;
490 	psmouse->resync_time = 0;
491 
492 	__set_bit(INPUT_PROP_POINTER, dev->propbit);
493 	/* Touchpad */
494 	__set_bit(BTN_TOUCH, dev->keybit);
495 	__set_bit(BTN_TOOL_FINGER, dev->keybit);
496 	/* Buttons */
497 	__set_bit(BTN_LEFT, dev->keybit);
498 	__set_bit(BTN_RIGHT, dev->keybit);
499 	__clear_bit(BTN_MIDDLE, dev->keybit);
500 
501 	/* Absolute position */
502 	__set_bit(EV_ABS, dev->evbit);
503 	input_set_abs_params(dev, ABS_X, 0, BYD_PAD_WIDTH, 0, 0);
504 	input_set_abs_params(dev, ABS_Y, 0, BYD_PAD_HEIGHT, 0, 0);
505 	input_abs_set_res(dev, ABS_X, BYD_PAD_RESOLUTION);
506 	input_abs_set_res(dev, ABS_Y, BYD_PAD_RESOLUTION);
507 	/* No relative support */
508 	__clear_bit(EV_REL, dev->evbit);
509 	__clear_bit(REL_X, dev->relbit);
510 	__clear_bit(REL_Y, dev->relbit);
511 
512 	return 0;
513 }
514