byd.c 13 KB

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