touchpad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* Elan HID-over-I2C touchpad on the Braswell LPSS I2C bus.
  2. *
  3. * The HP Chromebook 11 G5 EE (setzer) wires its touchpad to LPSS I2C6 at
  4. * 0xD1327000, slave address 0x15. The LPSS I2C functions are hidden from
  5. * PCI in ACPI mode, so the driver scans the two firmware BAR windows for
  6. * DesignWare component types instead of enumerating PCI.
  7. *
  8. * The report line is a GPIO we have no driver for, so the input register is
  9. * polled. A read with no pending report returns a zero length immediately.
  10. * Reading the register back-to-back stalls the device: 25 ms (40 Hz) is the
  11. * rate it keeps up with, and the pointer still moves smoothly.
  12. *
  13. * Init follows Linux's elan_i2c: reset, consume the 2-byte reset
  14. * acknowledgement, read the HID descriptor and the max-X/Y commands, select
  15. * absolute mode (0x0300) and wake the device (0x0800).
  16. *
  17. * Report decoding is Elan-specific; the contact behaviour (pointer deltas,
  18. * taps, the physical-button latch and two-finger scrolling) lives in
  19. * touchpad_gesture.c so another touchpad driver can reuse it.
  20. */
  21. #include "touchpad.h"
  22. #include "touchpad_gesture.h"
  23. #include "i2c.h"
  24. #include "io.h"
  25. #include "paging.h"
  26. #include "timer.h"
  27. #define TP_ADDR 0x15
  28. #define TP_CTRL_MAX 16
  29. #define TP_CTRL_STEP 0x1000u
  30. #define TP_HID_DESC_REG 0x0001
  31. #define TP_HID_DESC_LEN 30
  32. #define TP_CMD_REG 0x0005
  33. #define TP_CMD_RESET 0x0100
  34. #define TP_CMD_WAKE 0x0800
  35. #define TP_MODE_REG 0x0300
  36. #define TP_MODE_ABS 0x0001
  37. #define TP_DW_COMP_TYPE 0x44570140u
  38. #define TP_REPORT_ID 0x5d
  39. #define TP_MAX_X_REG 0x0106
  40. #define TP_MAX_Y_REG 0x0107
  41. #define TP_ABS_MAX 4095
  42. #define TP_MAX_INPUT 64
  43. #define TP_POLL_MS 25
  44. static unsigned int tp_base;
  45. static unsigned int tp_addr = TP_ADDR;
  46. static int tp_present;
  47. static unsigned int tp_in_reg = 0x0003;
  48. static unsigned int tp_max_in = 34;
  49. static unsigned int tp_cmd_reg = TP_CMD_REG;
  50. static unsigned short tp_vendor;
  51. static unsigned short tp_product;
  52. static int tp_max_x = TP_ABS_MAX;
  53. static int tp_max_y = TP_ABS_MAX;
  54. static const char *tp_err;
  55. static unsigned int tp_err_val;
  56. static struct tpg_state tp_gesture;
  57. static int tp_last_buttons;
  58. static unsigned int tp_next_poll;
  59. /* Last report, for the `touchpad` status command. */
  60. static int tp_stat_fingers, tp_stat_f1, tp_stat_x, tp_stat_y;
  61. static void put_dec(unsigned int v);
  62. static const char *rc_name(void);
  63. static void put_hex16(unsigned int v)
  64. {
  65. static const char hex[] = "0123456789ABCDEF";
  66. io_putchar(hex[(v >> 12) & 0xf]);
  67. io_putchar(hex[(v >> 8) & 0xf]);
  68. io_putchar(hex[(v >> 4) & 0xf]);
  69. io_putchar(hex[v & 0xf]);
  70. }
  71. static void print_summary(void)
  72. {
  73. io_putstr("touchpad: elan ");
  74. put_hex16(tp_vendor);
  75. io_putchar(':');
  76. put_hex16(tp_product);
  77. io_putstr(" i2c 0x");
  78. io_puthex(tp_base);
  79. io_putstr(" addr 0x");
  80. io_puthex(tp_addr);
  81. io_putstr(" max ");
  82. put_dec((unsigned int)tp_max_x);
  83. io_putchar('x');
  84. put_dec((unsigned int)tp_max_y);
  85. io_putchar('\n');
  86. }
  87. static void print_status(void)
  88. {
  89. unsigned int opts = tpg_options(&tp_gesture);
  90. print_summary();
  91. io_putstr("touchpad: rightclick ");
  92. io_putstr(rc_name());
  93. io_putstr(" tapclick ");
  94. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  95. io_putstr(" scroll ");
  96. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  97. if (opts & TPG_SCROLL) {
  98. io_putchar(' ');
  99. io_putstr((opts & TPG_SCROLL_INVERT) ?
  100. "traditional" : "natural");
  101. }
  102. io_putstr(" corner x>=");
  103. put_dec((unsigned int)(tp_max_x * TPG_CORNER_X_PCT / 100));
  104. io_putstr(" y<=");
  105. put_dec((unsigned int)(tp_max_y * TPG_CORNER_Y_PCT / 100));
  106. io_putchar('\n');
  107. io_putstr("touchpad: last f=");
  108. put_dec((unsigned int)tp_stat_fingers);
  109. if (tp_stat_f1) {
  110. io_putstr(" x=");
  111. put_dec((unsigned int)tp_stat_x);
  112. io_putstr(" y=");
  113. put_dec((unsigned int)tp_stat_y);
  114. }
  115. io_putstr(" taps left=");
  116. put_dec(tp_gesture.taps_left);
  117. io_putstr(" right=");
  118. put_dec(tp_gesture.taps_right);
  119. io_putchar('\n');
  120. }
  121. static void msleep(unsigned int ms)
  122. {
  123. unsigned int t0 = get_ticks_ms();
  124. while (get_ticks_ms() - t0 < ms)
  125. ;
  126. }
  127. static unsigned int get16(const unsigned char *p)
  128. {
  129. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  130. }
  131. static void put_dec(unsigned int v)
  132. {
  133. char buf[11];
  134. unsigned int i = 0;
  135. if (!v) {
  136. io_putchar('0');
  137. return;
  138. }
  139. while (v) {
  140. buf[i++] = (char)('0' + v % 10);
  141. v /= 10;
  142. }
  143. while (i)
  144. io_putchar(buf[--i]);
  145. }
  146. /* Match one whitespace-delimited word at s (end or space after it). */
  147. static int word_is(const char *s, const char *word)
  148. {
  149. while (*word) {
  150. if (*s != *word)
  151. return 0;
  152. s++;
  153. word++;
  154. }
  155. return *s == '\0' || *s == ' ';
  156. }
  157. static const char *skip_spaces(const char *s)
  158. {
  159. while (*s == ' ')
  160. s++;
  161. return s;
  162. }
  163. static unsigned int word_len(const char *s)
  164. {
  165. unsigned int n = 0;
  166. while (s[n] && s[n] != ' ')
  167. n++;
  168. return n;
  169. }
  170. static const char *rc_name(void)
  171. {
  172. unsigned int m = tpg_options(&tp_gesture) &
  173. (TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  174. if (m == TPG_RIGHT_TAP)
  175. return "tap";
  176. if (m == TPG_RIGHT_CORNER)
  177. return "corner";
  178. if (m == (TPG_RIGHT_TAP | TPG_RIGHT_CORNER))
  179. return "both";
  180. return "off";
  181. }
  182. static int hid_read_reg(unsigned int addr, unsigned int reg,
  183. unsigned char *buf, unsigned int len)
  184. {
  185. unsigned char r[2];
  186. r[0] = (unsigned char)(reg & 0xff);
  187. r[1] = (unsigned char)(reg >> 8);
  188. if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
  189. return 0;
  190. /* Some devices do not answer the combined write+read (repeated
  191. * start). Retry with a STOP between the register address and the
  192. * data read. */
  193. if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
  194. return -1;
  195. return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
  196. }
  197. static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
  198. {
  199. unsigned char b[4];
  200. b[0] = (unsigned char)(reg & 0xff);
  201. b[1] = (unsigned char)(reg >> 8);
  202. b[2] = (unsigned char)(val & 0xff);
  203. b[3] = (unsigned char)(val >> 8);
  204. return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
  205. }
  206. /* Native Elan max-coordinate commands (the same read-cmd path Linux
  207. * elan_i2c uses). They give the corner zone a physical frame instead of
  208. * assuming the full 12-bit range; on failure keep the 4095 fallback. */
  209. static void query_limits(void)
  210. {
  211. unsigned char v[2];
  212. if (hid_read_reg(tp_addr, TP_MAX_X_REG, v, 2) == 0) {
  213. unsigned int m = get16(v) & 0x0fff;
  214. if (m >= 256)
  215. tp_max_x = (int)m;
  216. }
  217. if (hid_read_reg(tp_addr, TP_MAX_Y_REG, v, 2) == 0) {
  218. unsigned int m = get16(v) & 0x0fff;
  219. if (m >= 256)
  220. tp_max_y = (int)m;
  221. }
  222. }
  223. static int probe_controller(unsigned int base)
  224. {
  225. unsigned char desc[TP_HID_DESC_LEN + 2];
  226. unsigned char ack[2];
  227. unsigned int off, len, ver;
  228. if (!i2c_dw_present(base))
  229. return 0;
  230. if (i2c_dw_init(base) != 0) {
  231. tp_err = "i2c init failed";
  232. return 0;
  233. }
  234. tp_base = base;
  235. /* The Elan address is fixed; a plain read is a harmless ACK check. */
  236. if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
  237. tp_err = "no device at 0x15";
  238. return 0;
  239. }
  240. /* Reset first and consume the acknowledgement, then read the HID
  241. * descriptor. The descriptor read is prefixed with two status
  242. * bytes and the 2-byte length, so accept either offset. */
  243. hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
  244. msleep(150);
  245. (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
  246. if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
  247. tp_err = "descriptor read failed";
  248. tp_err_val = i2c_last_abort;
  249. return 0;
  250. }
  251. off = 0;
  252. if (get16(desc + 0) != TP_HID_DESC_LEN ||
  253. get16(desc + 2) != 0x0100) {
  254. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  255. get16(desc + 4) == 0x0100)
  256. off = 2;
  257. }
  258. len = get16(desc + off);
  259. ver = get16(desc + off + 2);
  260. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  261. tp_err = "bad hid descriptor";
  262. return 0;
  263. }
  264. tp_in_reg = get16(desc + off + 8);
  265. tp_max_in = get16(desc + off + 10);
  266. tp_cmd_reg = get16(desc + off + 16);
  267. tp_vendor = (unsigned short)get16(desc + off + 20);
  268. tp_product = (unsigned short)get16(desc + off + 22);
  269. if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
  270. tp_max_in = 34;
  271. /* Absolute reporting, then wake: after the reset the Elan stays
  272. * quiet until the host selects a mode. */
  273. hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
  274. msleep(40);
  275. hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
  276. msleep(60);
  277. /* Query the limits with the device awake (Linux reads them after
  278. * initialize()/wake and before enabling reports). */
  279. query_limits();
  280. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  281. tp_present = 1;
  282. tp_err = 0;
  283. print_summary();
  284. return 1;
  285. }
  286. int touchpad_init(void)
  287. {
  288. /* The ACPI _CRS address for the LPSS I2C blocks is 0x9132xxxx on
  289. * coreboot, but this firmware maps them at 0xD132xxxx. Both
  290. * windows are scanned; page 7 (I2C6) is tried first. */
  291. static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
  292. unsigned int r, k;
  293. if (tp_present)
  294. return 1;
  295. for (r = 0; r < 2; r++) {
  296. for (k = 0; k < TP_CTRL_MAX; k++) {
  297. unsigned int i = (k + 7) % TP_CTRL_MAX;
  298. unsigned int base = ranges[r] + i * TP_CTRL_STEP;
  299. paging_map_mmio(base, 0x1000);
  300. /* A block left in functional reset keeps its
  301. * register file but never clocks the I2C FSM. */
  302. i2c_dw_reset_release(base);
  303. if (probe_controller(base))
  304. return 1;
  305. }
  306. }
  307. return 0;
  308. }
  309. int touchpad_present(void)
  310. {
  311. return tp_present;
  312. }
  313. unsigned int touchpad_addr(void)
  314. {
  315. return tp_addr;
  316. }
  317. unsigned short touchpad_vendor(void)
  318. {
  319. return tp_vendor;
  320. }
  321. unsigned short touchpad_product(void)
  322. {
  323. return tp_product;
  324. }
  325. int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
  326. {
  327. unsigned char buf[TP_MAX_INPUT];
  328. unsigned int now, b;
  329. struct tpg_result res;
  330. *buttons = 0;
  331. *dx = 0;
  332. *dy = 0;
  333. *scroll = 0;
  334. if (!tp_present)
  335. return 0;
  336. now = get_ticks_ms();
  337. /* The synthetic tap press is released by time, whether or not a new
  338. * report arrives (the finger is already up). */
  339. tpg_tick(&tp_gesture, now, &res);
  340. if (res.buttons != (unsigned int)tp_last_buttons) {
  341. tp_last_buttons = (int)res.buttons;
  342. *buttons = res.buttons;
  343. return 1;
  344. }
  345. if ((int)(now - tp_next_poll) < 0)
  346. return 0;
  347. tp_next_poll = now + TP_POLL_MS;
  348. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  349. return 0;
  350. /* The addressed read returns two status bytes, the 2-byte length
  351. * and then the report, so locate the 0x5D report ID instead of
  352. * trusting a fixed offset. Layout after it (Linux hid-elan.c):
  353. * touch info (f5..f1 at bits 7..3, clickpad at bit 0), then 5
  354. * bytes per *active* finger (packed): x11:8/y11:9, x7:0, y7:0,
  355. * size, pressure. */
  356. for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
  357. if (buf[b] == TP_REPORT_ID)
  358. break;
  359. if (b >= 6 || b + 6 >= tp_max_in)
  360. return 0;
  361. {
  362. struct tpg_frame f;
  363. unsigned int ti = buf[b + 1];
  364. unsigned int off = b + 2;
  365. unsigned int i, n = 0;
  366. f.count = 0;
  367. f.mask = 0;
  368. f.button = ti & 0x01;
  369. f.now_ms = now;
  370. for (i = 0; i < 5; i++) {
  371. if (!(ti & (0x08u << i)))
  372. continue;
  373. f.count++;
  374. if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
  375. unsigned int xh = buf[off];
  376. f.contact[n].x = ((int)(xh & 0xf0) << 4) |
  377. buf[off + 1];
  378. f.contact[n].y = ((int)(xh & 0x0f) << 8) |
  379. buf[off + 2];
  380. f.mask |= 1u << n;
  381. n++;
  382. }
  383. off += 5;
  384. }
  385. tp_stat_fingers = (int)f.count;
  386. tp_stat_f1 = (f.mask & 1u) != 0;
  387. if (f.mask & 1u) {
  388. tp_stat_x = f.contact[0].x;
  389. tp_stat_y = f.contact[0].y;
  390. }
  391. tpg_frame(&tp_gesture, &f, &res);
  392. }
  393. *buttons = res.buttons;
  394. *dx = res.dx;
  395. *dy = res.dy;
  396. *scroll = res.scroll;
  397. if (res.buttons != (unsigned int)tp_last_buttons) {
  398. tp_last_buttons = (int)res.buttons;
  399. return 1;
  400. }
  401. return res.dx || res.dy || res.scroll;
  402. }
  403. void program_touchpad_main(const char *arg)
  404. {
  405. if (!tp_present) {
  406. io_putstr("touchpad: absent (");
  407. io_putstr(tp_err ? tp_err : "unknown");
  408. if (tp_err_val) {
  409. io_putstr(" ab=");
  410. io_puthex(tp_err_val);
  411. }
  412. io_putstr(")\n");
  413. return;
  414. }
  415. arg = arg ? skip_spaces(arg) : 0;
  416. if (!arg || !*arg) {
  417. print_status();
  418. return;
  419. }
  420. while (*arg) {
  421. unsigned int opts = tpg_options(&tp_gesture);
  422. if (word_is(arg, "rightclick")) {
  423. const char *v = skip_spaces(arg + 10);
  424. if (!*v) {
  425. io_putstr("touchpad: rightclick ");
  426. io_putstr(rc_name());
  427. io_putchar('\n');
  428. return;
  429. }
  430. opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  431. if (word_is(v, "off")) {
  432. /* keep both bits clear */
  433. } else if (word_is(v, "tap")) {
  434. opts |= TPG_RIGHT_TAP;
  435. } else if (word_is(v, "corner")) {
  436. opts |= TPG_RIGHT_CORNER;
  437. } else if (word_is(v, "both")) {
  438. opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
  439. } else {
  440. io_putstr("touchpad: rightclick off|tap|corner|both\n");
  441. return;
  442. }
  443. tpg_set_options(&tp_gesture, opts);
  444. io_putstr("touchpad: rightclick ");
  445. io_putstr(rc_name());
  446. io_putchar('\n');
  447. arg = skip_spaces(v + word_len(v));
  448. } else if (word_is(arg, "tapclick")) {
  449. const char *v = skip_spaces(arg + 8);
  450. if (!*v) {
  451. io_putstr("touchpad: tapclick ");
  452. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  453. io_putchar('\n');
  454. return;
  455. }
  456. if (word_is(v, "on"))
  457. opts |= TPG_TAPCLICK;
  458. else if (word_is(v, "off"))
  459. opts &= ~TPG_TAPCLICK;
  460. else {
  461. io_putstr("touchpad: tapclick on|off\n");
  462. return;
  463. }
  464. tpg_set_options(&tp_gesture, opts);
  465. io_putstr("touchpad: tapclick ");
  466. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  467. io_putchar('\n');
  468. arg = skip_spaces(v + word_len(v));
  469. } else if (word_is(arg, "scroll")) {
  470. const char *v = skip_spaces(arg + 6);
  471. if (!*v) {
  472. io_putstr("touchpad: scroll ");
  473. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  474. if (opts & TPG_SCROLL) {
  475. io_putchar(' ');
  476. io_putstr((opts & TPG_SCROLL_INVERT) ?
  477. "traditional" : "natural");
  478. }
  479. io_putchar('\n');
  480. return;
  481. }
  482. if (word_is(v, "on")) {
  483. opts |= TPG_SCROLL;
  484. } else if (word_is(v, "off")) {
  485. opts &= ~TPG_SCROLL;
  486. } else if (word_is(v, "natural")) {
  487. opts |= TPG_SCROLL;
  488. opts &= ~TPG_SCROLL_INVERT;
  489. } else if (word_is(v, "traditional")) {
  490. opts |= TPG_SCROLL;
  491. opts |= TPG_SCROLL_INVERT;
  492. } else {
  493. io_putstr("touchpad: scroll on|off|natural|traditional\n");
  494. return;
  495. }
  496. tpg_set_options(&tp_gesture, opts);
  497. io_putstr("touchpad: scroll ");
  498. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  499. if (opts & TPG_SCROLL) {
  500. io_putchar(' ');
  501. io_putstr((opts & TPG_SCROLL_INVERT) ?
  502. "traditional" : "natural");
  503. }
  504. io_putchar('\n');
  505. arg = skip_spaces(v + word_len(v));
  506. } else {
  507. io_putstr("touchpad: usage: touchpad [rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional]\n");
  508. return;
  509. }
  510. }
  511. }