byd.c 13 KB

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