byd.c 13 KB

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