byd.c 13 KB

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