touchpad.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /* Touchpads on the LPSS I2C buses.
  2. *
  3. * Two HID-over-I2C families are supported:
  4. * - Elan on the HP Chromebook 11 G5 EE (Braswell LPSS I2C6 at 0xD1327000,
  5. * slave 0x15, vendor report 0x5D);
  6. * - Synaptics 06CB:7F8F on the Lenovo Yoga 720-13IKB (Sunrise Point-LP
  7. * LPSS I2C0 at PCI 00:15.0, slave 0x2C, Windows-Precision report 3:
  8. * five 5-byte finger records, scan time, contact count and clickpad).
  9. *
  10. * The LPSS I2C functions are hidden from PCI in ACPI mode on Braswell, so
  11. * that path scans the two firmware BAR windows for DesignWare component
  12. * types; the Skylake/Kaby Lake path finds the PCI 00:15.0 function and uses
  13. * its BAR.
  14. *
  15. * The report line is a GPIO we have no driver for, so the input register is
  16. * polled. A read with no pending report returns a zero length immediately.
  17. * Reading the register back-to-back stalls the device: 25 ms (40 Hz) is the
  18. * rate it keeps up with, and the pointer still moves smoothly.
  19. *
  20. * Init follows the HID-over-I2C descriptor and the vendor wake sequence.
  21. * Report decoding is per-family; the contact behaviour (pointer deltas,
  22. * taps, the physical-button latch and two-finger scrolling) lives in
  23. * touchpad_gesture.c so the backends share it.
  24. */
  25. #include "cpu.h"
  26. #include "touchpad.h"
  27. #include "touchpad_gesture.h"
  28. #include "i2c.h"
  29. #include "io.h"
  30. #include "paging.h"
  31. #include "pci.h"
  32. #include "timer.h"
  33. #define TP_ADDR 0x15
  34. #define TP_SYN_ADDR 0x2c
  35. #define TP_CTRL_MAX 16
  36. #define TP_CTRL_STEP 0x1000u
  37. #define TP_HID_DESC_REG 0x0001
  38. #define TP_HID_DESC_LEN 30
  39. #define TP_CMD_REG 0x0005
  40. #define TP_CMD_RESET 0x0100
  41. #define TP_CMD_WAKE 0x0800
  42. #define TP_MODE_REG 0x0300
  43. #define TP_MODE_ABS 0x0001
  44. #define TP_DW_COMP_TYPE 0x44570140u
  45. #define TP_REPORT_ID 0x5d
  46. #define TP_SYN_REPORT_ID 3
  47. #define TP_SYN_REPORT_LEN 30
  48. #define TP_SYN_MAX_X 1219
  49. #define TP_SYN_MAX_Y 799
  50. #define TP_MAX_X_REG 0x0106
  51. #define TP_MAX_Y_REG 0x0107
  52. #define TP_ABS_MAX 4095
  53. #define TP_MAX_INPUT 64
  54. #define TP_POLL_MS 25
  55. enum {
  56. TP_KIND_NONE,
  57. TP_KIND_ELAN,
  58. TP_KIND_SYNAPTICS,
  59. };
  60. static unsigned int tp_base;
  61. static unsigned int tp_addr = TP_ADDR;
  62. static int tp_kind;
  63. static int tp_present;
  64. static int tp_invert_y;
  65. static unsigned int tp_in_reg = 0x0003;
  66. static unsigned int tp_max_in = 34;
  67. static unsigned int tp_cmd_reg = TP_CMD_REG;
  68. static unsigned short tp_vendor;
  69. static unsigned short tp_product;
  70. static int tp_max_x = TP_ABS_MAX;
  71. static int tp_max_y = TP_ABS_MAX;
  72. static const char *tp_err;
  73. static unsigned int tp_err_val;
  74. /* Probe diagnostics for `touchpad` when the controller/device is absent. */
  75. static unsigned int tp_dbg_id, tp_dbg_bar, tp_dbg_cmd, tp_dbg_comp;
  76. static unsigned int tp_dbg_priv0, tp_dbg_priv4;
  77. static struct tpg_state tp_gesture;
  78. static int tp_last_buttons;
  79. static unsigned int tp_next_poll;
  80. /* Last report, for the `touchpad` status command. */
  81. static int tp_stat_fingers, tp_stat_f1, tp_stat_x, tp_stat_y;
  82. static void put_dec(unsigned int v);
  83. static const char *rc_name(void);
  84. static void put_hex16(unsigned int v)
  85. {
  86. static const char hex[] = "0123456789ABCDEF";
  87. io_putchar(hex[(v >> 12) & 0xf]);
  88. io_putchar(hex[(v >> 8) & 0xf]);
  89. io_putchar(hex[(v >> 4) & 0xf]);
  90. io_putchar(hex[v & 0xf]);
  91. }
  92. static void print_summary(void)
  93. {
  94. io_putstr("touchpad: ");
  95. io_putstr(tp_kind == TP_KIND_SYNAPTICS ? "synaptics " : "elan ");
  96. put_hex16(tp_vendor);
  97. io_putchar(':');
  98. put_hex16(tp_product);
  99. io_putstr(" i2c 0x");
  100. io_puthex(tp_base);
  101. io_putstr(" addr 0x");
  102. io_puthex(tp_addr);
  103. io_putstr(" max ");
  104. put_dec((unsigned int)tp_max_x);
  105. io_putchar('x');
  106. put_dec((unsigned int)tp_max_y);
  107. io_putchar('\n');
  108. }
  109. static void print_status(void)
  110. {
  111. unsigned int opts = tpg_options(&tp_gesture);
  112. print_summary();
  113. io_putstr("touchpad: rightclick ");
  114. io_putstr(rc_name());
  115. io_putstr(" tapclick ");
  116. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  117. io_putstr(" scroll ");
  118. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  119. if (opts & TPG_SCROLL) {
  120. io_putchar(' ');
  121. io_putstr((opts & TPG_SCROLL_INVERT) ?
  122. "traditional" : "natural");
  123. }
  124. io_putstr(" invert ");
  125. io_putstr(tp_invert_y ? "on" : "off");
  126. io_putstr(" corner x>=");
  127. put_dec((unsigned int)(tp_max_x * TPG_CORNER_X_PCT / 100));
  128. io_putstr(" y<=");
  129. put_dec((unsigned int)(tp_max_y * TPG_CORNER_Y_PCT / 100));
  130. io_putchar('\n');
  131. io_putstr("touchpad: last f=");
  132. put_dec((unsigned int)tp_stat_fingers);
  133. if (tp_stat_f1) {
  134. io_putstr(" x=");
  135. put_dec((unsigned int)tp_stat_x);
  136. io_putstr(" y=");
  137. put_dec((unsigned int)tp_stat_y);
  138. }
  139. io_putstr(" taps left=");
  140. put_dec(tp_gesture.taps_left);
  141. io_putstr(" right=");
  142. put_dec(tp_gesture.taps_right);
  143. io_putchar('\n');
  144. }
  145. static void msleep(unsigned int ms)
  146. {
  147. unsigned int t0 = get_ticks_ms();
  148. while (get_ticks_ms() - t0 < ms)
  149. ;
  150. }
  151. static unsigned int get16(const unsigned char *p)
  152. {
  153. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  154. }
  155. static void put_dec(unsigned int v)
  156. {
  157. char buf[11];
  158. unsigned int i = 0;
  159. if (!v) {
  160. io_putchar('0');
  161. return;
  162. }
  163. while (v) {
  164. buf[i++] = (char)('0' + v % 10);
  165. v /= 10;
  166. }
  167. while (i)
  168. io_putchar(buf[--i]);
  169. }
  170. /* Match one whitespace-delimited word at s (end or space after it). */
  171. static int word_is(const char *s, const char *word)
  172. {
  173. while (*word) {
  174. if (*s != *word)
  175. return 0;
  176. s++;
  177. word++;
  178. }
  179. return *s == '\0' || *s == ' ';
  180. }
  181. static const char *skip_spaces(const char *s)
  182. {
  183. while (*s == ' ')
  184. s++;
  185. return s;
  186. }
  187. static unsigned int word_len(const char *s)
  188. {
  189. unsigned int n = 0;
  190. while (s[n] && s[n] != ' ')
  191. n++;
  192. return n;
  193. }
  194. static const char *rc_name(void)
  195. {
  196. unsigned int m = tpg_options(&tp_gesture) &
  197. (TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  198. if (m == TPG_RIGHT_TAP)
  199. return "tap";
  200. if (m == TPG_RIGHT_CORNER)
  201. return "corner";
  202. if (m == (TPG_RIGHT_TAP | TPG_RIGHT_CORNER))
  203. return "both";
  204. return "off";
  205. }
  206. static int hid_read_reg(unsigned int addr, unsigned int reg,
  207. unsigned char *buf, unsigned int len)
  208. {
  209. unsigned char r[2];
  210. r[0] = (unsigned char)(reg & 0xff);
  211. r[1] = (unsigned char)(reg >> 8);
  212. if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
  213. return 0;
  214. /* Some devices do not answer the combined write+read (repeated
  215. * start). Retry with a STOP between the register address and the
  216. * data read. */
  217. if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
  218. return -1;
  219. return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
  220. }
  221. static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
  222. {
  223. unsigned char b[4];
  224. b[0] = (unsigned char)(reg & 0xff);
  225. b[1] = (unsigned char)(reg >> 8);
  226. b[2] = (unsigned char)(val & 0xff);
  227. b[3] = (unsigned char)(val >> 8);
  228. return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
  229. }
  230. /* Native Elan max-coordinate commands (the same read-cmd path Linux
  231. * elan_i2c uses). They give the corner zone a physical frame instead of
  232. * assuming the full 12-bit range; on failure keep the 4095 fallback. */
  233. static void query_limits(void)
  234. {
  235. unsigned char v[2];
  236. if (hid_read_reg(tp_addr, TP_MAX_X_REG, v, 2) == 0) {
  237. unsigned int m = get16(v) & 0x0fff;
  238. if (m >= 256)
  239. tp_max_x = (int)m;
  240. }
  241. if (hid_read_reg(tp_addr, TP_MAX_Y_REG, v, 2) == 0) {
  242. unsigned int m = get16(v) & 0x0fff;
  243. if (m >= 256)
  244. tp_max_y = (int)m;
  245. }
  246. }
  247. static int probe_controller(unsigned int base)
  248. {
  249. unsigned char desc[TP_HID_DESC_LEN + 2];
  250. unsigned char ack[2];
  251. unsigned int off, len, ver;
  252. if (!i2c_dw_present(base))
  253. return 0;
  254. if (i2c_dw_init(base) != 0) {
  255. tp_err = "i2c init failed";
  256. return 0;
  257. }
  258. tp_base = base;
  259. /* The Elan address is fixed; a plain read is a harmless ACK check. */
  260. if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
  261. tp_err = "no device at 0x15";
  262. return 0;
  263. }
  264. /* Reset first and consume the acknowledgement, then read the HID
  265. * descriptor. The descriptor read is prefixed with two status
  266. * bytes and the 2-byte length, so accept either offset. */
  267. hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
  268. msleep(150);
  269. (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
  270. if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
  271. tp_err = "descriptor read failed";
  272. tp_err_val = i2c_last_abort;
  273. return 0;
  274. }
  275. off = 0;
  276. if (get16(desc + 0) != TP_HID_DESC_LEN ||
  277. get16(desc + 2) != 0x0100) {
  278. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  279. get16(desc + 4) == 0x0100)
  280. off = 2;
  281. }
  282. len = get16(desc + off);
  283. ver = get16(desc + off + 2);
  284. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  285. tp_err = "bad hid descriptor";
  286. return 0;
  287. }
  288. tp_in_reg = get16(desc + off + 8);
  289. tp_max_in = get16(desc + off + 10);
  290. tp_cmd_reg = get16(desc + off + 16);
  291. tp_vendor = (unsigned short)get16(desc + off + 20);
  292. tp_product = (unsigned short)get16(desc + off + 22);
  293. if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
  294. tp_max_in = 34;
  295. /* Absolute reporting, then wake: after the reset the Elan stays
  296. * quiet until the host selects a mode. */
  297. hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
  298. msleep(40);
  299. hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
  300. msleep(60);
  301. /* Query the limits with the device awake (Linux reads them after
  302. * initialize()/wake and before enabling reports). */
  303. query_limits();
  304. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  305. tp_present = 1;
  306. tp_kind = TP_KIND_ELAN;
  307. tp_addr = TP_ADDR;
  308. tp_invert_y = 0;
  309. tp_err = 0;
  310. print_summary();
  311. return 1;
  312. }
  313. /* Synaptics 06CB:7F8F Windows-Precision touchpad (Yoga 720-13IKB) on the
  314. * Sunrise Point-LP LPSS I2C0. The HID descriptor is the standard 30-byte
  315. * HID-over-I2C block; report 3 is five 5-byte finger records (tip/confidence,
  316. * contact id, 16-bit X, 16-bit Y), then scan time, contact count and the
  317. * clickpad button. */
  318. static int probe_synaptics(unsigned int base)
  319. {
  320. unsigned char desc[TP_HID_DESC_LEN + 2];
  321. unsigned char ack[2];
  322. unsigned int off, len, ver, i;
  323. int tried_power = 0;
  324. if (i2c_dw_init(base) != 0) {
  325. tp_err = "i2c init failed";
  326. return 0;
  327. }
  328. tp_base = base;
  329. tp_addr = TP_SYN_ADDR;
  330. tp_dbg_priv0 = *(volatile unsigned int *)(base + 0x200);
  331. tp_dbg_priv4 = *(volatile unsigned int *)(base + 0x204);
  332. /* Linux's i2c-hid first checks the address and reads the HID
  333. * descriptor; only the report-descriptor stage powers the device on
  334. * and resets it. Do the same, with a power-on/retry fallback for a
  335. * device left asleep by a previous OS. */
  336. if (i2c_dw_transfer(base, tp_addr, 0, 0, ack, 1) != 0) {
  337. tp_err = "no device at 0x2c";
  338. tp_err_val = i2c_last_abort;
  339. return 0;
  340. }
  341. for (i = 0; i < 4; i++) {
  342. if (hid_read_reg(tp_addr, TP_HID_DESC_REG, desc,
  343. sizeof(desc)) == 0)
  344. break;
  345. if (!tried_power) {
  346. tried_power = 1;
  347. hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_WAKE);
  348. msleep(60);
  349. } else {
  350. hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_RESET);
  351. msleep(200);
  352. }
  353. }
  354. if (i == 4) {
  355. tp_err = "descriptor read failed";
  356. tp_err_val = i2c_last_abort;
  357. return 0;
  358. }
  359. off = 0;
  360. len = get16(desc + 0);
  361. ver = get16(desc + 2);
  362. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  363. if (get16(desc + 2) == TP_HID_DESC_LEN &&
  364. get16(desc + 4) == 0x0100)
  365. off = 2;
  366. len = get16(desc + off);
  367. ver = get16(desc + off + 2);
  368. }
  369. if (len != TP_HID_DESC_LEN || ver != 0x0100) {
  370. tp_err = "bad hid descriptor";
  371. return 0;
  372. }
  373. tp_in_reg = get16(desc + off + 8);
  374. tp_max_in = get16(desc + off + 10);
  375. tp_cmd_reg = get16(desc + off + 16);
  376. tp_vendor = (unsigned short)get16(desc + off + 20);
  377. tp_product = (unsigned short)get16(desc + off + 22);
  378. if (tp_vendor != 0x06CB || tp_product != 0x7F8F) {
  379. tp_err = "not a Synaptics 06CB:7F8F";
  380. return 0;
  381. }
  382. if (tp_max_in < TP_SYN_REPORT_LEN + 2 || tp_max_in > TP_MAX_INPUT)
  383. tp_max_in = TP_SYN_REPORT_LEN + 2;
  384. /* i2c_hid_hwreset(): power on (60 ms), reset, then wait for the
  385. * zero-length reset-complete input report before using the device. */
  386. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
  387. msleep(60);
  388. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_RESET);
  389. for (i = 0; i < 40; i++) {
  390. unsigned char z[TP_MAX_INPUT];
  391. if (i2c_dw_transfer(base, tp_addr, 0, 0, z, tp_max_in) == 0 &&
  392. z[0] == 0 && z[1] == 0)
  393. break;
  394. msleep(25);
  395. }
  396. hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
  397. msleep(60);
  398. tp_kind = TP_KIND_SYNAPTICS;
  399. /* The Windows-Precision report uses the logical frame from its
  400. * descriptor. Default to Y-down (the usual HID digitizer frame);
  401. * `touchpad invert off` fixes a unit that reports Y-up. */
  402. tp_invert_y = 1;
  403. tp_max_x = TP_SYN_MAX_X;
  404. tp_max_y = TP_SYN_MAX_Y;
  405. tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
  406. tp_present = 1;
  407. tp_err = 0;
  408. print_summary();
  409. return 1;
  410. }
  411. /* Sunrise Point-LP leaves the LPSS functions with memory decode off: Linux
  412. * logs "intel-lpss 0000:00:15.0: enabling device (0000 -> 0002)" before it
  413. * can even read the DesignWare component type. A warm reboot from another
  414. * OS can additionally leave the function in D3hot, so put it back in D0.
  415. * Both steps are no-ops when firmware already did them. */
  416. static void lpss_pci_prepare(unsigned int dev, unsigned int func)
  417. {
  418. unsigned int cmd, cap, pm;
  419. cmd = pci_cfg_read32(0, dev, func, 0x04) & 0xFFFFu;
  420. pci_cfg_write16(0, dev, func, 0x04,
  421. (unsigned short)(cmd | 0x0006u));
  422. cap = pci_cfg_read32(0, dev, func, 0x34) & 0xFFu;
  423. if (cap >= 0x40 && (cap & 3) == 0) {
  424. pm = (pci_cfg_read32(0, dev, func, cap) >> 16) & 0xFFFFu;
  425. pci_cfg_write16(0, dev, func, cap + 4,
  426. (unsigned short)(pm & ~3u));
  427. }
  428. msleep(10);
  429. }
  430. int touchpad_init(void)
  431. {
  432. /* The ACPI _CRS address for the Braswell LPSS I2C blocks is
  433. * 0x9132xxxx on coreboot, but this firmware maps them at 0xD132xxxx.
  434. * Both windows are scanned; page 7 (I2C6) is tried first. */
  435. static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
  436. unsigned int r, k;
  437. if (tp_present)
  438. return 1;
  439. tp_err = 0;
  440. tp_err_val = 0;
  441. /* Braswell (HP Chromebook 11 G5 EE): LPSS I2C6 is hidden from PCI. */
  442. if (cpu_family_model() == 0x64Cu) {
  443. for (r = 0; r < 2; r++) {
  444. for (k = 0; k < TP_CTRL_MAX; k++) {
  445. unsigned int i = (k + 7) % TP_CTRL_MAX;
  446. unsigned int base = (unsigned int)ioremap(
  447. ranges[r] + i * TP_CTRL_STEP, 0x1000,
  448. IOREMAP_MMIO);
  449. if (!base)
  450. continue;
  451. /* A block left in functional reset keeps its
  452. * register file but never clocks the I2C FSM. */
  453. i2c_dw_reset_release(base);
  454. if (probe_controller(base))
  455. return 1;
  456. }
  457. }
  458. }
  459. /* Sunrise Point-LP LPSS I2C0/1/2 (Skylake/Kaby Lake): the functions
  460. * are visible at PCI 00:15.0/1/2. The Yoga's Synaptics touchpad is
  461. * on I2C0 at 0x2C; the Wacom touchscreen on I2C1 is rejected by the
  462. * vendor/product check in probe_synaptics(). */
  463. for (k = 0; k < 3; k++) {
  464. unsigned int id = pci_cfg_read32(0, 0x15, k, 0x00);
  465. unsigned int bar, base;
  466. if (id != 0x9d608086u && id != 0x9d618086u &&
  467. id != 0x9d628086u)
  468. continue;
  469. lpss_pci_prepare(0x15, k);
  470. bar = pci_cfg_read32(0, 0x15, k, 0x10) & ~0xFu;
  471. if (!bar)
  472. continue;
  473. base = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
  474. if (!base)
  475. continue;
  476. /* Release the LPSS function reset before reading the
  477. * DesignWare component type: the register file may answer
  478. * while the block is still in functional reset, and the
  479. * I2C FSM never clocks until 0x204 is released. */
  480. i2c_dw_reset_release(base);
  481. tp_dbg_id = id;
  482. tp_dbg_bar = bar;
  483. tp_dbg_cmd = pci_cfg_read32(0, 0x15, k, 0x04) & 0xFFFFu;
  484. tp_dbg_comp = i2c_dw_comp_type(base);
  485. if (!i2c_dw_present(base)) {
  486. tp_err = "LPSS not a DesignWare I2C";
  487. tp_err_val = tp_dbg_comp;
  488. continue;
  489. }
  490. if (probe_synaptics(base))
  491. return 1;
  492. }
  493. if (!tp_err)
  494. tp_err = "no LPSS I2C touchpad";
  495. return 0;
  496. }
  497. int touchpad_present(void)
  498. {
  499. return tp_present;
  500. }
  501. unsigned int touchpad_addr(void)
  502. {
  503. return tp_addr;
  504. }
  505. unsigned short touchpad_vendor(void)
  506. {
  507. return tp_vendor;
  508. }
  509. unsigned short touchpad_product(void)
  510. {
  511. return tp_product;
  512. }
  513. static int elan_decode(const unsigned char *buf, struct tpg_frame *f,
  514. unsigned int now)
  515. {
  516. unsigned int b, ti, off, i, n = 0;
  517. for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
  518. if (buf[b] == TP_REPORT_ID)
  519. break;
  520. if (b >= 6 || b + 6 >= tp_max_in)
  521. return 0;
  522. ti = buf[b + 1];
  523. off = b + 2;
  524. f->count = 0;
  525. f->mask = 0;
  526. f->button = ti & 0x01;
  527. f->now_ms = now;
  528. for (i = 0; i < 5; i++) {
  529. if (!(ti & (0x08u << i)))
  530. continue;
  531. f->count++;
  532. if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
  533. unsigned int xh = buf[off];
  534. f->contact[n].x = ((int)(xh & 0xf0) << 4) |
  535. buf[off + 1];
  536. f->contact[n].y = ((int)(xh & 0x0f) << 8) |
  537. buf[off + 2];
  538. if (tp_invert_y)
  539. f->contact[n].y = tp_max_y - 1 -
  540. f->contact[n].y;
  541. f->mask |= 1u << n;
  542. n++;
  543. }
  544. off += 5;
  545. }
  546. return 1;
  547. }
  548. /* Windows-Precision report 3: five 5-byte finger records, then 2-byte scan
  549. * time, contact count and the clickpad button bit. */
  550. static int synaptics_decode(const unsigned char *buf, struct tpg_frame *f,
  551. unsigned int now)
  552. {
  553. unsigned int b, p, i, n = 0;
  554. for (b = 0; b < 6 && b + TP_SYN_REPORT_LEN <= tp_max_in; b++)
  555. if (buf[b] == TP_SYN_REPORT_ID)
  556. break;
  557. if (b >= 6 || b + TP_SYN_REPORT_LEN > tp_max_in)
  558. return 0;
  559. p = b + 1;
  560. f->count = buf[b + 28];
  561. f->mask = 0;
  562. f->button = buf[b + 29] & 1;
  563. f->now_ms = now;
  564. for (i = 0; i < 5; i++, p += 5) {
  565. unsigned int f0 = buf[p];
  566. int x, y;
  567. if (!(f0 & 0x01))
  568. continue;
  569. x = (int)get16(buf + p + 1);
  570. y = (int)get16(buf + p + 3);
  571. if (tp_invert_y)
  572. y = tp_max_y - 1 - y;
  573. if (n < TPG_CONTACTS) {
  574. f->contact[n].x = x;
  575. f->contact[n].y = y;
  576. f->mask |= 1u << n;
  577. n++;
  578. }
  579. }
  580. if (f->count > 5)
  581. f->count = 5;
  582. return 1;
  583. }
  584. int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
  585. {
  586. unsigned char buf[TP_MAX_INPUT];
  587. unsigned int now;
  588. struct tpg_result res;
  589. *buttons = 0;
  590. *dx = 0;
  591. *dy = 0;
  592. *scroll = 0;
  593. if (!tp_present)
  594. return 0;
  595. now = get_ticks_ms();
  596. /* The synthetic tap press is released by time, whether or not a new
  597. * report arrives (the finger is already up). */
  598. tpg_tick(&tp_gesture, now, &res);
  599. if (res.buttons != (unsigned int)tp_last_buttons) {
  600. tp_last_buttons = (int)res.buttons;
  601. *buttons = res.buttons;
  602. return 1;
  603. }
  604. if ((int)(now - tp_next_poll) < 0)
  605. return 0;
  606. tp_next_poll = now + TP_POLL_MS;
  607. if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
  608. return 0;
  609. {
  610. struct tpg_frame f;
  611. if (tp_kind == TP_KIND_SYNAPTICS) {
  612. if (!synaptics_decode(buf, &f, now))
  613. return 0;
  614. } else {
  615. if (!elan_decode(buf, &f, now))
  616. return 0;
  617. }
  618. tp_stat_fingers = (int)f.count;
  619. tp_stat_f1 = (f.mask & 1u) != 0;
  620. if (f.mask & 1u) {
  621. tp_stat_x = f.contact[0].x;
  622. tp_stat_y = f.contact[0].y;
  623. }
  624. tpg_frame(&tp_gesture, &f, &res);
  625. }
  626. *buttons = res.buttons;
  627. *dx = res.dx;
  628. *dy = res.dy;
  629. *scroll = res.scroll;
  630. if (res.buttons != (unsigned int)tp_last_buttons) {
  631. tp_last_buttons = (int)res.buttons;
  632. return 1;
  633. }
  634. return res.dx || res.dy || res.scroll;
  635. }
  636. static void print_absent(void)
  637. {
  638. io_putstr("touchpad: absent (");
  639. io_putstr(tp_err ? tp_err : "unknown");
  640. if (tp_err_val) {
  641. io_putstr(" val=");
  642. io_puthex(tp_err_val);
  643. }
  644. io_putstr(")\n");
  645. if (tp_dbg_id) {
  646. io_putstr("touchpad: lpss id ");
  647. io_puthex(tp_dbg_id);
  648. io_putstr(" bar 0x");
  649. io_puthex(tp_dbg_bar);
  650. io_putstr(" cmd ");
  651. io_puthex(tp_dbg_cmd);
  652. io_putstr(" comp ");
  653. io_puthex(tp_dbg_comp);
  654. io_putstr(" priv ");
  655. io_puthex(tp_dbg_priv0);
  656. io_putchar('/');
  657. io_puthex(tp_dbg_priv4);
  658. io_putchar('\n');
  659. }
  660. }
  661. void program_touchpad_main(const char *arg)
  662. {
  663. arg = arg ? skip_spaces(arg) : 0;
  664. if (arg && word_is(arg, "probe")) {
  665. if (!tp_present)
  666. touchpad_init();
  667. if (tp_present)
  668. print_status();
  669. else
  670. print_absent();
  671. return;
  672. }
  673. if (!tp_present) {
  674. print_absent();
  675. return;
  676. }
  677. arg = arg ? skip_spaces(arg) : 0;
  678. if (!arg || !*arg) {
  679. print_status();
  680. return;
  681. }
  682. while (*arg) {
  683. unsigned int opts = tpg_options(&tp_gesture);
  684. if (word_is(arg, "rightclick")) {
  685. const char *v = skip_spaces(arg + 10);
  686. if (!*v) {
  687. io_putstr("touchpad: rightclick ");
  688. io_putstr(rc_name());
  689. io_putchar('\n');
  690. return;
  691. }
  692. opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
  693. if (word_is(v, "off")) {
  694. /* keep both bits clear */
  695. } else if (word_is(v, "tap")) {
  696. opts |= TPG_RIGHT_TAP;
  697. } else if (word_is(v, "corner")) {
  698. opts |= TPG_RIGHT_CORNER;
  699. } else if (word_is(v, "both")) {
  700. opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
  701. } else {
  702. io_putstr("touchpad: rightclick off|tap|corner|both\n");
  703. return;
  704. }
  705. tpg_set_options(&tp_gesture, opts);
  706. io_putstr("touchpad: rightclick ");
  707. io_putstr(rc_name());
  708. io_putchar('\n');
  709. arg = skip_spaces(v + word_len(v));
  710. } else if (word_is(arg, "tapclick")) {
  711. const char *v = skip_spaces(arg + 8);
  712. if (!*v) {
  713. io_putstr("touchpad: tapclick ");
  714. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  715. io_putchar('\n');
  716. return;
  717. }
  718. if (word_is(v, "on"))
  719. opts |= TPG_TAPCLICK;
  720. else if (word_is(v, "off"))
  721. opts &= ~TPG_TAPCLICK;
  722. else {
  723. io_putstr("touchpad: tapclick on|off\n");
  724. return;
  725. }
  726. tpg_set_options(&tp_gesture, opts);
  727. io_putstr("touchpad: tapclick ");
  728. io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
  729. io_putchar('\n');
  730. arg = skip_spaces(v + word_len(v));
  731. } else if (word_is(arg, "scroll")) {
  732. const char *v = skip_spaces(arg + 6);
  733. if (!*v) {
  734. io_putstr("touchpad: scroll ");
  735. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  736. if (opts & TPG_SCROLL) {
  737. io_putchar(' ');
  738. io_putstr((opts & TPG_SCROLL_INVERT) ?
  739. "traditional" : "natural");
  740. }
  741. io_putchar('\n');
  742. return;
  743. }
  744. if (word_is(v, "on")) {
  745. opts |= TPG_SCROLL;
  746. } else if (word_is(v, "off")) {
  747. opts &= ~TPG_SCROLL;
  748. } else if (word_is(v, "natural")) {
  749. opts |= TPG_SCROLL;
  750. opts &= ~TPG_SCROLL_INVERT;
  751. } else if (word_is(v, "traditional")) {
  752. opts |= TPG_SCROLL;
  753. opts |= TPG_SCROLL_INVERT;
  754. } else {
  755. io_putstr("touchpad: scroll on|off|natural|traditional\n");
  756. return;
  757. }
  758. tpg_set_options(&tp_gesture, opts);
  759. io_putstr("touchpad: scroll ");
  760. io_putstr((opts & TPG_SCROLL) ? "on" : "off");
  761. if (opts & TPG_SCROLL) {
  762. io_putchar(' ');
  763. io_putstr((opts & TPG_SCROLL_INVERT) ?
  764. "traditional" : "natural");
  765. }
  766. io_putchar('\n');
  767. arg = skip_spaces(v + word_len(v));
  768. } else if (word_is(arg, "invert")) {
  769. const char *v = skip_spaces(arg + 6);
  770. if (!*v) {
  771. io_putstr("touchpad: invert ");
  772. io_putstr(tp_invert_y ? "on" : "off");
  773. io_putchar('\n');
  774. return;
  775. }
  776. if (word_is(v, "on"))
  777. tp_invert_y = 1;
  778. else if (word_is(v, "off"))
  779. tp_invert_y = 0;
  780. else {
  781. io_putstr("touchpad: invert on|off\n");
  782. return;
  783. }
  784. io_putstr("touchpad: invert ");
  785. io_putstr(tp_invert_y ? "on" : "off");
  786. io_putchar('\n');
  787. arg = skip_spaces(v + word_len(v));
  788. } else {
  789. io_putstr("touchpad: usage: touchpad [probe | rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional | invert on|off]\n");
  790. return;
  791. }
  792. }
  793. }