| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- /* Touchpads on the LPSS I2C buses.
- *
- * Two HID-over-I2C families are supported:
- * - Elan on the HP Chromebook 11 G5 EE (Braswell LPSS I2C6 at 0xD1327000,
- * slave 0x15, vendor report 0x5D);
- * - Synaptics 06CB:7F8F on the Lenovo Yoga 720-13IKB (Sunrise Point-LP
- * LPSS I2C0 at PCI 00:15.0, slave 0x2C, Windows-Precision report 3:
- * five 5-byte finger records, scan time, contact count and clickpad).
- *
- * The LPSS I2C functions are hidden from PCI in ACPI mode on Braswell, so
- * that path scans the two firmware BAR windows for DesignWare component
- * types; the Skylake/Kaby Lake path finds the PCI 00:15.0 function and uses
- * its BAR.
- *
- * The report line is a GPIO we have no driver for, so the input register is
- * polled. A read with no pending report returns a zero length immediately.
- * Reading the register back-to-back stalls the device: 25 ms (40 Hz) is the
- * rate it keeps up with, and the pointer still moves smoothly.
- *
- * Init follows the HID-over-I2C descriptor and the vendor wake sequence.
- * Report decoding is per-family; the contact behaviour (pointer deltas,
- * taps, the physical-button latch and two-finger scrolling) lives in
- * touchpad_gesture.c so the backends share it.
- */
- #include "cpu.h"
- #include "touchpad.h"
- #include "touchpad_gesture.h"
- #include "i2c.h"
- #include "ideapad.h"
- #include "io.h"
- #include "paging.h"
- #include "pci.h"
- #include "serial.h"
- #include "timer.h"
- #define TP_ADDR 0x15
- #define TP_SYN_ADDR 0x2c
- #define TP_CTRL_MAX 16
- #define TP_CTRL_STEP 0x1000u
- #define TP_HID_DESC_REG 0x0001
- #define TP_HID_DESC_LEN 30
- /* TPD1._DSM(HID-I2C UUID, 1) in the Yoga DSDT: the descriptor register. */
- #define TP_SYN_DESC_REG 0x0020
- #define TP_CMD_REG 0x0005
- #define TP_CMD_RESET 0x0100
- #define TP_CMD_WAKE 0x0800
- #define TP_MODE_REG 0x0300
- #define TP_MODE_ABS 0x0001
- #define TP_DW_COMP_TYPE 0x44570140u
- #define TP_REPORT_ID 0x5d
- #define TP_SYN_REPORT_ID 3
- #define TP_SYN_REPORT_LEN 30
- #define TP_SYN_MAX_X 1219
- #define TP_SYN_MAX_Y 799
- /* Sunrise Point-LP GPIO community 1 (GPP_C) and the I2C0 pads. The
- * DSDT builds the community windows as SBRG + 0xAE0000 (community 1),
- * +0xAF0000 (0) and +0xAC0000 (2); P2SB's fixed 16 MB MMIO window on
- * this PCH starts at 0xFD000000. */
- #define TP_MAX_X_REG 0x0106
- #define TP_MAX_Y_REG 0x0107
- #define TP_ABS_MAX 4095
- #define TP_MAX_INPUT 64
- #define TP_POLL_MS 25
- enum {
- TP_KIND_NONE,
- TP_KIND_ELAN,
- TP_KIND_SYNAPTICS,
- };
- static unsigned int tp_base;
- static unsigned int tp_addr = TP_ADDR;
- static int tp_kind;
- static int tp_present;
- static int tp_invert_y;
- static unsigned int tp_in_reg = 0x0003;
- static unsigned int tp_max_in = 34;
- static unsigned int tp_cmd_reg = TP_CMD_REG;
- static unsigned short tp_vendor;
- static unsigned short tp_product;
- static int tp_max_x = TP_ABS_MAX;
- static int tp_max_y = TP_ABS_MAX;
- static const char *tp_err;
- static unsigned int tp_err_val;
- /* Probe diagnostics for `touchpad` when the controller/device is absent. */
- static unsigned int tp_dbg_id, tp_dbg_bar, tp_dbg_cmd, tp_dbg_comp;
- static unsigned int tp_dbg_priv0, tp_dbg_priv4;
- static unsigned int tp_dbg_pmec, tp_dbg_pmec_after;
- static int tp_dbg_seen;
- static struct tpg_state tp_gesture;
- static int tp_last_buttons;
- static unsigned int tp_next_poll;
- /* Last report, for the `touchpad` status command. */
- static int tp_stat_fingers, tp_stat_f1, tp_stat_x, tp_stat_y;
- static void put_dec(unsigned int v);
- static const char *rc_name(void);
- static void put_hex16(unsigned int v)
- {
- static const char hex[] = "0123456789ABCDEF";
- io_putchar(hex[(v >> 12) & 0xf]);
- io_putchar(hex[(v >> 8) & 0xf]);
- io_putchar(hex[(v >> 4) & 0xf]);
- io_putchar(hex[v & 0xf]);
- }
- static void print_summary(void)
- {
- io_putstr("touchpad: ");
- io_putstr(tp_kind == TP_KIND_SYNAPTICS ? "synaptics " : "elan ");
- put_hex16(tp_vendor);
- io_putchar(':');
- put_hex16(tp_product);
- io_putstr(" i2c 0x");
- io_puthex(tp_base);
- io_putstr(" addr 0x");
- io_puthex(tp_addr);
- io_putstr(" max ");
- put_dec((unsigned int)tp_max_x);
- io_putchar('x');
- put_dec((unsigned int)tp_max_y);
- io_putchar('\n');
- }
- static void print_status(void)
- {
- unsigned int opts = tpg_options(&tp_gesture);
- print_summary();
- io_putstr("touchpad: rightclick ");
- io_putstr(rc_name());
- io_putstr(" tapclick ");
- io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
- io_putstr(" scroll ");
- io_putstr((opts & TPG_SCROLL) ? "on" : "off");
- if (opts & TPG_SCROLL) {
- io_putchar(' ');
- io_putstr((opts & TPG_SCROLL_INVERT) ?
- "traditional" : "natural");
- }
- io_putstr(" invert ");
- io_putstr(tp_invert_y ? "on" : "off");
- io_putstr(" corner x>=");
- put_dec((unsigned int)(tp_max_x * TPG_CORNER_X_PCT / 100));
- io_putstr(" y<=");
- put_dec((unsigned int)(tp_max_y * TPG_CORNER_Y_PCT / 100));
- io_putchar('\n');
- io_putstr("touchpad: last f=");
- put_dec((unsigned int)tp_stat_fingers);
- if (tp_stat_f1) {
- io_putstr(" x=");
- put_dec((unsigned int)tp_stat_x);
- io_putstr(" y=");
- put_dec((unsigned int)tp_stat_y);
- }
- io_putstr(" taps left=");
- put_dec(tp_gesture.taps_left);
- io_putstr(" right=");
- put_dec(tp_gesture.taps_right);
- io_putchar('\n');
- }
- static void msleep(unsigned int ms)
- {
- unsigned int t0 = get_ticks_ms();
- while (get_ticks_ms() - t0 < ms)
- ;
- }
- static unsigned int get16(const unsigned char *p)
- {
- return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
- }
- static void put_dec(unsigned int v)
- {
- char buf[11];
- unsigned int i = 0;
- if (!v) {
- io_putchar('0');
- return;
- }
- while (v) {
- buf[i++] = (char)('0' + v % 10);
- v /= 10;
- }
- while (i)
- io_putchar(buf[--i]);
- }
- /* Match one whitespace-delimited word at s (end or space after it). */
- static int word_is(const char *s, const char *word)
- {
- while (*word) {
- if (*s != *word)
- return 0;
- s++;
- word++;
- }
- return *s == '\0' || *s == ' ';
- }
- static const char *skip_spaces(const char *s)
- {
- while (*s == ' ')
- s++;
- return s;
- }
- static unsigned int word_len(const char *s)
- {
- unsigned int n = 0;
- while (s[n] && s[n] != ' ')
- n++;
- return n;
- }
- static const char *rc_name(void)
- {
- unsigned int m = tpg_options(&tp_gesture) &
- (TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
- if (m == TPG_RIGHT_TAP)
- return "tap";
- if (m == TPG_RIGHT_CORNER)
- return "corner";
- if (m == (TPG_RIGHT_TAP | TPG_RIGHT_CORNER))
- return "both";
- return "off";
- }
- static int hid_read_reg(unsigned int addr, unsigned int reg,
- unsigned char *buf, unsigned int len)
- {
- unsigned char r[2];
- r[0] = (unsigned char)(reg & 0xff);
- r[1] = (unsigned char)(reg >> 8);
- if (i2c_dw_transfer(tp_base, addr, r, 2, buf, len) == 0)
- return 0;
- /* Some devices do not answer the combined write+read (repeated
- * start). Retry with a STOP between the register address and the
- * data read. */
- if (i2c_dw_transfer(tp_base, addr, r, 2, 0, 0) != 0)
- return -1;
- return i2c_dw_transfer(tp_base, addr, 0, 0, buf, len);
- }
- static int hid_write_reg(unsigned int addr, unsigned int reg, unsigned int val)
- {
- unsigned char b[4];
- b[0] = (unsigned char)(reg & 0xff);
- b[1] = (unsigned char)(reg >> 8);
- b[2] = (unsigned char)(val & 0xff);
- b[3] = (unsigned char)(val >> 8);
- return i2c_dw_transfer(tp_base, addr, b, 4, 0, 0);
- }
- /* Native Elan max-coordinate commands (the same read-cmd path Linux
- * elan_i2c uses). They give the corner zone a physical frame instead of
- * assuming the full 12-bit range; on failure keep the 4095 fallback. */
- static void query_limits(void)
- {
- unsigned char v[2];
- if (hid_read_reg(tp_addr, TP_MAX_X_REG, v, 2) == 0) {
- unsigned int m = get16(v) & 0x0fff;
- if (m >= 256)
- tp_max_x = (int)m;
- }
- if (hid_read_reg(tp_addr, TP_MAX_Y_REG, v, 2) == 0) {
- unsigned int m = get16(v) & 0x0fff;
- if (m >= 256)
- tp_max_y = (int)m;
- }
- }
- static int probe_controller(unsigned int base)
- {
- unsigned char desc[TP_HID_DESC_LEN + 2];
- unsigned char ack[2];
- unsigned int off, len, ver;
- if (!i2c_dw_present(base))
- return 0;
- if (i2c_dw_init(base) != 0) {
- tp_err = "i2c init failed";
- return 0;
- }
- tp_base = base;
- /* The Elan address is fixed; a plain read is a harmless ACK check. */
- if (i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 1) != 0) {
- tp_err = "no device at 0x15";
- return 0;
- }
- /* Reset first and consume the acknowledgement, then read the HID
- * descriptor. The descriptor read is prefixed with two status
- * bytes and the 2-byte length, so accept either offset. */
- hid_write_reg(TP_ADDR, TP_CMD_REG, TP_CMD_RESET);
- msleep(150);
- (void)i2c_dw_transfer(base, TP_ADDR, 0, 0, ack, 2);
- if (hid_read_reg(TP_ADDR, TP_HID_DESC_REG, desc, sizeof(desc)) != 0) {
- tp_err = "descriptor read failed";
- tp_err_val = i2c_last_abort;
- return 0;
- }
- off = 0;
- if (get16(desc + 0) != TP_HID_DESC_LEN ||
- get16(desc + 2) != 0x0100) {
- if (get16(desc + 2) == TP_HID_DESC_LEN &&
- get16(desc + 4) == 0x0100)
- off = 2;
- }
- len = get16(desc + off);
- ver = get16(desc + off + 2);
- if (len != TP_HID_DESC_LEN || ver != 0x0100) {
- tp_err = "bad hid descriptor";
- return 0;
- }
- tp_in_reg = get16(desc + off + 8);
- tp_max_in = get16(desc + off + 10);
- tp_cmd_reg = get16(desc + off + 16);
- tp_vendor = (unsigned short)get16(desc + off + 20);
- tp_product = (unsigned short)get16(desc + off + 22);
- if (tp_max_in < 12 || tp_max_in > TP_MAX_INPUT)
- tp_max_in = 34;
- /* Absolute reporting, then wake: after the reset the Elan stays
- * quiet until the host selects a mode. */
- hid_write_reg(TP_ADDR, TP_MODE_REG, TP_MODE_ABS);
- msleep(40);
- hid_write_reg(TP_ADDR, tp_cmd_reg, TP_CMD_WAKE);
- msleep(60);
- /* Query the limits with the device awake (Linux reads them after
- * initialize()/wake and before enabling reports). */
- query_limits();
- tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
- tp_present = 1;
- tp_kind = TP_KIND_ELAN;
- tp_addr = TP_ADDR;
- tp_invert_y = 0;
- tp_err = 0;
- print_summary();
- return 1;
- }
- /* HID-over-I2C SET_REPORT (opcode 3) of a one-byte feature report: the
- * command word carries type 3 (feature) and the report id, the data register
- * gets a 2-byte length (including itself), the id and the value. */
- static void syn_set_feature(unsigned int data_reg, unsigned int id,
- unsigned int val)
- {
- unsigned char b[10];
- b[0] = (unsigned char)(tp_cmd_reg & 0xff);
- b[1] = (unsigned char)(tp_cmd_reg >> 8);
- b[2] = (unsigned char)(0x30 | id);
- b[3] = 0x03;
- b[4] = (unsigned char)(data_reg & 0xff);
- b[5] = (unsigned char)(data_reg >> 8);
- b[6] = 4;
- b[7] = 0;
- b[8] = (unsigned char)id;
- b[9] = (unsigned char)val;
- (void)i2c_dw_transfer(tp_base, tp_addr, b, 10, 0, 0);
- msleep(10);
- }
- /* Synaptics 06CB:7F8F Windows-Precision touchpad (Yoga 720-13IKB) on the
- * Sunrise Point-LP LPSS I2C0. The HID descriptor is the standard 30-byte
- * HID-over-I2C block; report 3 is five 5-byte finger records (tip/confidence,
- * contact id, 16-bit X, 16-bit Y), then scan time, contact count and the
- * clickpad button. */
- static int probe_synaptics(unsigned int base, unsigned int tries)
- {
- unsigned char desc[TP_HID_DESC_LEN + 2];
- unsigned char ack[2];
- unsigned int off, len, ver, i;
- int tried_power = 0;
- if (i2c_dw_init(base) != 0) {
- tp_err = "i2c init failed";
- return 0;
- }
- tp_base = base;
- tp_addr = TP_SYN_ADDR;
- if (!tp_dbg_seen) {
- tp_dbg_priv0 = *(volatile unsigned int *)(base + 0x200);
- tp_dbg_priv4 = *(volatile unsigned int *)(base + 0x204);
- }
- /* Linux's i2c-hid first checks the address and reads the HID
- * descriptor; only the report-descriptor stage powers the device on
- * and resets it. Do the same, with a power-on/retry fallback for a
- * device left asleep by a previous OS. */
- /* The EC releases the touchpad once ideapad_os_ready() told it an
- * ACPI OS is running; give it up to a second on I2C0. */
- while (i2c_dw_transfer(base, tp_addr, 0, 0, ack, 1) != 0) {
- if (--tries) {
- msleep(50);
- continue;
- }
- tp_err = "no device at 0x2c";
- tp_err_val = i2c_last_abort;
- i2c_dw_dump(base);
- return 0;
- }
- for (i = 0; i < 4; i++) {
- if (hid_read_reg(tp_addr, TP_SYN_DESC_REG, desc,
- sizeof(desc)) == 0)
- break;
- if (!tried_power) {
- tried_power = 1;
- hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_WAKE);
- msleep(60);
- } else {
- hid_write_reg(tp_addr, TP_CMD_REG, TP_CMD_RESET);
- msleep(200);
- }
- }
- if (i == 4) {
- tp_err = "descriptor read failed";
- tp_err_val = i2c_last_abort;
- return 0;
- }
- off = 0;
- len = get16(desc + 0);
- ver = get16(desc + 2);
- if (len != TP_HID_DESC_LEN || ver != 0x0100) {
- if (get16(desc + 2) == TP_HID_DESC_LEN &&
- get16(desc + 4) == 0x0100)
- off = 2;
- len = get16(desc + off);
- ver = get16(desc + off + 2);
- }
- if (len != TP_HID_DESC_LEN || ver != 0x0100) {
- tp_err = "bad hid descriptor";
- return 0;
- }
- tp_in_reg = get16(desc + off + 8);
- tp_max_in = get16(desc + off + 10);
- tp_cmd_reg = get16(desc + off + 16);
- tp_vendor = (unsigned short)get16(desc + off + 20);
- tp_product = (unsigned short)get16(desc + off + 22);
- if (tp_vendor != 0x06CB || tp_product != 0x7F8F) {
- tp_err = "not a Synaptics 06CB:7F8F";
- return 0;
- }
- if (tp_max_in < TP_SYN_REPORT_LEN + 2 || tp_max_in > TP_MAX_INPUT)
- tp_max_in = TP_SYN_REPORT_LEN + 2;
- /* i2c_hid_hwreset(): power on (60 ms), reset, then wait for the
- * zero-length reset-complete input report before using the device. */
- hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
- msleep(60);
- hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_RESET);
- for (i = 0; i < 40; i++) {
- unsigned char z[TP_MAX_INPUT];
- if (i2c_dw_transfer(base, tp_addr, 0, 0, z, tp_max_in) == 0 &&
- z[0] == 0 && z[1] == 0)
- break;
- msleep(25);
- }
- hid_write_reg(tp_addr, tp_cmd_reg, TP_CMD_WAKE);
- msleep(60);
- /* A Windows Precision Touchpad reports as a plain mouse (report 2)
- * until the host selects touchpad mode, like hid-multitouch's
- * mt_set_modes(): feature 4 Input Mode = 3, then feature 6 surface
- * and button switches on. */
- syn_set_feature(get16(desc + off + 18), 4, 3);
- syn_set_feature(get16(desc + off + 18), 6, 3);
- tp_kind = TP_KIND_SYNAPTICS;
- /* The Windows-Precision report uses the logical frame from its
- * descriptor. Default to Y-down (the usual HID digitizer frame);
- * `touchpad invert off` fixes a unit that reports Y-up. */
- tp_invert_y = 1;
- tp_max_x = TP_SYN_MAX_X;
- tp_max_y = TP_SYN_MAX_Y;
- tpg_init(&tp_gesture, tp_max_x, tp_max_y, 2);
- tp_present = 1;
- tp_err = 0;
- print_summary();
- return 1;
- }
- /* Sunrise Point-LP leaves the LPSS functions with memory decode off: Linux
- * logs "intel-lpss 0000:00:15.0: enabling device (0000 -> 0002)" before it
- * can even read the DesignWare component type. A warm reboot from another
- * OS can additionally leave the function in D3hot, so put it back in D0.
- * Both steps are no-ops when firmware already did them. */
- static void lpss_pci_prepare(unsigned int dev, unsigned int func)
- {
- unsigned int cmd, pmcsr;
- cmd = pci_cfg_read32(0, dev, func, 0x04) & 0xFFFFu;
- pci_cfg_write16(0, dev, func, 0x04,
- (unsigned short)(cmd | 0x0006u));
- /* The DSDT's I2C0._PS0() is LPD0(SB10), which clears bits 15 and 1
- * of the LPSS PMECTRLSTATUS at config offset 0x84 through the P2SB
- * alias. The same register is directly reachable as PCI config
- * 0x84. Writing the PMC's PWRMBASE + 0x84 instead (0xFE000084)
- * left the LPSS island power-gated: the DesignWare register file
- * answered, but the I2C FSM never drove the bus, so the touchpad
- * NAKed its address. */
- pmcsr = pci_cfg_read32(0, dev, func, 0x84);
- if (!tp_dbg_seen)
- tp_dbg_pmec = pmcsr;
- if (pmcsr != 0xFFFFFFFFu) {
- pci_cfg_write32(0, dev, func, 0x84, pmcsr & 0xFFFF7FFCu);
- if (!tp_dbg_seen)
- tp_dbg_pmec_after = pci_cfg_read32(0, dev, func,
- 0x84);
- }
- msleep(10);
- }
- int touchpad_init(void)
- {
- /* The ACPI _CRS address for the Braswell LPSS I2C blocks is
- * 0x9132xxxx on coreboot, but this firmware maps them at 0xD132xxxx.
- * Both windows are scanned; page 7 (I2C6) is tried first. */
- static const unsigned int ranges[2] = { 0x91320000u, 0xd1320000u };
- unsigned int r, k;
- if (tp_present)
- return 1;
- tp_err = 0;
- tp_err_val = 0;
- /* Braswell (HP Chromebook 11 G5 EE): LPSS I2C6 is hidden from PCI. */
- if (cpu_family_model() == 0x64Cu) {
- for (r = 0; r < 2; r++) {
- for (k = 0; k < TP_CTRL_MAX; k++) {
- unsigned int i = (k + 7) % TP_CTRL_MAX;
- unsigned int base = (unsigned int)ioremap(
- ranges[r] + i * TP_CTRL_STEP, 0x1000,
- IOREMAP_MMIO);
- if (!base)
- continue;
- /* A block left in functional reset keeps its
- * register file but never clocks the I2C FSM. */
- i2c_dw_reset_release(base);
- if (probe_controller(base))
- return 1;
- }
- }
- }
- /* Sunrise Point-LP LPSS I2C0/1/2 (Skylake/Kaby Lake): the functions
- * are visible at PCI 00:15.0/1/2. The Yoga's Synaptics touchpad is
- * on I2C0 at 0x2C; the Wacom touchscreen on I2C1 is rejected by the
- * vendor/product check in probe_synaptics(). */
- for (k = 0; k < 3; k++) {
- unsigned int id = pci_cfg_read32(0, 0x15, k, 0x00);
- unsigned int bar, base;
- if (id != 0x9d608086u && id != 0x9d618086u &&
- id != 0x9d628086u)
- continue;
- lpss_pci_prepare(0x15, k);
- bar = pci_cfg_read32(0, 0x15, k, 0x10) & ~0xFu;
- if (!bar)
- continue;
- base = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
- if (!base)
- continue;
- /* Release the LPSS function reset before reading the
- * DesignWare component type: the register file may answer
- * while the block is still in functional reset, and the
- * I2C FSM never clocks until 0x204 is released. */
- i2c_dw_reset_release(base);
- if (!tp_dbg_seen) {
- tp_dbg_id = id;
- tp_dbg_bar = bar;
- tp_dbg_cmd = pci_cfg_read32(0, 0x15, k, 0x04) &
- 0xFFFFu;
- tp_dbg_comp = i2c_dw_comp_type(base);
- }
- if (!i2c_dw_present(base)) {
- tp_err = "LPSS not a DesignWare I2C";
- tp_err_val = tp_dbg_comp;
- tp_dbg_seen = 1;
- continue;
- }
- if (probe_synaptics(base, k == 0 && ideapad_present() ?
- 20 : 1))
- return 1;
- tp_dbg_seen = 1;
- }
- if (!tp_err)
- tp_err = "no LPSS I2C touchpad";
- return 0;
- }
- int touchpad_present(void)
- {
- return tp_present;
- }
- unsigned int touchpad_addr(void)
- {
- return tp_addr;
- }
- unsigned short touchpad_vendor(void)
- {
- return tp_vendor;
- }
- unsigned short touchpad_product(void)
- {
- return tp_product;
- }
- static int elan_decode(const unsigned char *buf, struct tpg_frame *f,
- unsigned int now)
- {
- unsigned int b, ti, off, i, n = 0;
- for (b = 0; b < 6 && b + 6 < tp_max_in; b++)
- if (buf[b] == TP_REPORT_ID)
- break;
- if (b >= 6 || b + 6 >= tp_max_in)
- return 0;
- ti = buf[b + 1];
- off = b + 2;
- f->count = 0;
- f->mask = 0;
- f->button = ti & 0x01;
- f->now_ms = now;
- for (i = 0; i < 5; i++) {
- if (!(ti & (0x08u << i)))
- continue;
- f->count++;
- if (n < TPG_CONTACTS && off + 4 < tp_max_in) {
- unsigned int xh = buf[off];
- f->contact[n].x = ((int)(xh & 0xf0) << 4) |
- buf[off + 1];
- f->contact[n].y = ((int)(xh & 0x0f) << 8) |
- buf[off + 2];
- if (tp_invert_y)
- f->contact[n].y = tp_max_y - 1 -
- f->contact[n].y;
- f->mask |= 1u << n;
- n++;
- }
- off += 5;
- }
- return 1;
- }
- /* Windows-Precision report 3: five 5-byte finger records, then 2-byte scan
- * time, contact count and the clickpad button bit. */
- static int synaptics_decode(const unsigned char *buf, struct tpg_frame *f,
- unsigned int now)
- {
- unsigned int b, p, i, n = 0;
- for (b = 0; b < 6 && b + TP_SYN_REPORT_LEN <= tp_max_in; b++)
- if (buf[b] == TP_SYN_REPORT_ID)
- break;
- if (b >= 6 || b + TP_SYN_REPORT_LEN > tp_max_in)
- return 0;
- p = b + 1;
- f->count = buf[b + 28];
- f->mask = 0;
- f->button = buf[b + 29] & 1;
- f->now_ms = now;
- for (i = 0; i < 5; i++, p += 5) {
- unsigned int f0 = buf[p];
- int x, y;
- /* Bit 0 is Confidence, bit 1 Tip Switch (usages 0x47, 0x42
- * in that order); a lift-off report keeps confidence set. */
- if (!(f0 & 0x02))
- continue;
- x = (int)get16(buf + p + 1);
- y = (int)get16(buf + p + 3);
- if (tp_invert_y)
- y = tp_max_y - 1 - y;
- if (n < TPG_CONTACTS) {
- f->contact[n].x = x;
- f->contact[n].y = y;
- f->mask |= 1u << n;
- n++;
- }
- }
- if (f->count > 5)
- f->count = 5;
- return 1;
- }
- int touchpad_poll(int *buttons, int *dx, int *dy, int *scroll)
- {
- unsigned char buf[TP_MAX_INPUT];
- unsigned int now;
- struct tpg_result res;
- *buttons = 0;
- *dx = 0;
- *dy = 0;
- *scroll = 0;
- if (!tp_present)
- return 0;
- now = get_ticks_ms();
- /* The synthetic tap press is released by time, whether or not a new
- * report arrives (the finger is already up). */
- tpg_tick(&tp_gesture, now, &res);
- if (res.buttons != (unsigned int)tp_last_buttons) {
- tp_last_buttons = (int)res.buttons;
- *buttons = res.buttons;
- return 1;
- }
- if ((int)(now - tp_next_poll) < 0)
- return 0;
- tp_next_poll = now + TP_POLL_MS;
- if (hid_read_reg(tp_addr, tp_in_reg, buf, tp_max_in) != 0)
- return 0;
- {
- struct tpg_frame f;
- if (tp_kind == TP_KIND_SYNAPTICS) {
- if (!synaptics_decode(buf, &f, now))
- return 0;
- } else {
- if (!elan_decode(buf, &f, now))
- return 0;
- }
- tp_stat_fingers = (int)f.count;
- tp_stat_f1 = (f.mask & 1u) != 0;
- if (f.mask & 1u) {
- tp_stat_x = f.contact[0].x;
- tp_stat_y = f.contact[0].y;
- }
- tpg_frame(&tp_gesture, &f, &res);
- }
- *buttons = res.buttons;
- *dx = res.dx;
- *dy = res.dy;
- *scroll = res.scroll;
- if (res.buttons != (unsigned int)tp_last_buttons) {
- tp_last_buttons = (int)res.buttons;
- return 1;
- }
- return res.dx || res.dy || res.scroll;
- }
- static void print_absent(void)
- {
- io_putstr("touchpad: absent (");
- io_putstr(tp_err ? tp_err : "unknown");
- if (tp_err_val) {
- io_putstr(" val=");
- io_puthex(tp_err_val);
- }
- io_putstr(")\n");
- if (tp_dbg_id) {
- io_putstr("touchpad: lpss id ");
- io_puthex(tp_dbg_id);
- io_putstr(" bar 0x");
- io_puthex(tp_dbg_bar);
- io_putstr(" cmd ");
- io_puthex(tp_dbg_cmd);
- io_putstr(" comp ");
- io_puthex(tp_dbg_comp);
- io_putstr(" priv ");
- io_puthex(tp_dbg_priv0);
- io_putchar('/');
- io_puthex(tp_dbg_priv4);
- io_putstr(" pmec ");
- io_puthex(tp_dbg_pmec);
- io_putchar('/');
- io_puthex(tp_dbg_pmec_after);
- io_putchar('\n');
- }
- }
- static void tp_serial_status(void)
- {
- serial_puts(SERIAL_COM1, "touchpad: ");
- if (tp_present) {
- serial_puts(SERIAL_COM1, tp_kind == TP_KIND_SYNAPTICS ?
- "present synaptics " : "present elan ");
- serial_puthex(SERIAL_COM1, tp_vendor);
- serial_putc(SERIAL_COM1, ':');
- serial_puthex(SERIAL_COM1, tp_product);
- serial_puts(SERIAL_COM1, " i2c 0x");
- serial_puthex(SERIAL_COM1, tp_base);
- serial_puts(SERIAL_COM1, " addr 0x");
- serial_puthex(SERIAL_COM1, tp_addr);
- } else {
- serial_puts(SERIAL_COM1, "absent ");
- serial_puts(SERIAL_COM1, tp_err ? tp_err : "unknown");
- serial_puts(SERIAL_COM1, " val=");
- serial_puthex(SERIAL_COM1, tp_err_val);
- if (tp_dbg_id) {
- serial_puts(SERIAL_COM1, " id=");
- serial_puthex(SERIAL_COM1, tp_dbg_id);
- serial_puts(SERIAL_COM1, " bar=");
- serial_puthex(SERIAL_COM1, tp_dbg_bar);
- serial_puts(SERIAL_COM1, " cmd=");
- serial_puthex(SERIAL_COM1, tp_dbg_cmd);
- serial_puts(SERIAL_COM1, " comp=");
- serial_puthex(SERIAL_COM1, tp_dbg_comp);
- serial_puts(SERIAL_COM1, " priv=");
- serial_puthex(SERIAL_COM1, tp_dbg_priv0);
- serial_putc(SERIAL_COM1, '/');
- serial_puthex(SERIAL_COM1, tp_dbg_priv4);
- serial_puts(SERIAL_COM1, " pmec=");
- serial_puthex(SERIAL_COM1, tp_dbg_pmec);
- serial_putc(SERIAL_COM1, '/');
- serial_puthex(SERIAL_COM1, tp_dbg_pmec_after);
- }
- }
- serial_putc(SERIAL_COM1, '\n');
- }
- void program_touchpad_main(const char *arg)
- {
- arg = arg ? skip_spaces(arg) : 0;
- if (arg && word_is(arg, "probe")) {
- if (!tp_present)
- touchpad_init();
- if (tp_present)
- print_status();
- else
- print_absent();
- tp_serial_status();
- return;
- }
- if (!tp_present) {
- print_absent();
- tp_serial_status();
- return;
- }
- arg = arg ? skip_spaces(arg) : 0;
- if (!arg || !*arg) {
- print_status();
- return;
- }
- while (*arg) {
- unsigned int opts = tpg_options(&tp_gesture);
- if (word_is(arg, "rightclick")) {
- const char *v = skip_spaces(arg + 10);
- if (!*v) {
- io_putstr("touchpad: rightclick ");
- io_putstr(rc_name());
- io_putchar('\n');
- return;
- }
- opts &= ~(TPG_RIGHT_TAP | TPG_RIGHT_CORNER);
- if (word_is(v, "off")) {
- /* keep both bits clear */
- } else if (word_is(v, "tap")) {
- opts |= TPG_RIGHT_TAP;
- } else if (word_is(v, "corner")) {
- opts |= TPG_RIGHT_CORNER;
- } else if (word_is(v, "both")) {
- opts |= TPG_RIGHT_TAP | TPG_RIGHT_CORNER;
- } else {
- io_putstr("touchpad: rightclick off|tap|corner|both\n");
- return;
- }
- tpg_set_options(&tp_gesture, opts);
- io_putstr("touchpad: rightclick ");
- io_putstr(rc_name());
- io_putchar('\n');
- arg = skip_spaces(v + word_len(v));
- } else if (word_is(arg, "tapclick")) {
- const char *v = skip_spaces(arg + 8);
- if (!*v) {
- io_putstr("touchpad: tapclick ");
- io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
- io_putchar('\n');
- return;
- }
- if (word_is(v, "on"))
- opts |= TPG_TAPCLICK;
- else if (word_is(v, "off"))
- opts &= ~TPG_TAPCLICK;
- else {
- io_putstr("touchpad: tapclick on|off\n");
- return;
- }
- tpg_set_options(&tp_gesture, opts);
- io_putstr("touchpad: tapclick ");
- io_putstr((opts & TPG_TAPCLICK) ? "on" : "off");
- io_putchar('\n');
- arg = skip_spaces(v + word_len(v));
- } else if (word_is(arg, "scroll")) {
- const char *v = skip_spaces(arg + 6);
- if (!*v) {
- io_putstr("touchpad: scroll ");
- io_putstr((opts & TPG_SCROLL) ? "on" : "off");
- if (opts & TPG_SCROLL) {
- io_putchar(' ');
- io_putstr((opts & TPG_SCROLL_INVERT) ?
- "traditional" : "natural");
- }
- io_putchar('\n');
- return;
- }
- if (word_is(v, "on")) {
- opts |= TPG_SCROLL;
- } else if (word_is(v, "off")) {
- opts &= ~TPG_SCROLL;
- } else if (word_is(v, "natural")) {
- opts |= TPG_SCROLL;
- opts &= ~TPG_SCROLL_INVERT;
- } else if (word_is(v, "traditional")) {
- opts |= TPG_SCROLL;
- opts |= TPG_SCROLL_INVERT;
- } else {
- io_putstr("touchpad: scroll on|off|natural|traditional\n");
- return;
- }
- tpg_set_options(&tp_gesture, opts);
- io_putstr("touchpad: scroll ");
- io_putstr((opts & TPG_SCROLL) ? "on" : "off");
- if (opts & TPG_SCROLL) {
- io_putchar(' ');
- io_putstr((opts & TPG_SCROLL_INVERT) ?
- "traditional" : "natural");
- }
- io_putchar('\n');
- arg = skip_spaces(v + word_len(v));
- } else if (word_is(arg, "invert")) {
- const char *v = skip_spaces(arg + 6);
- if (!*v) {
- io_putstr("touchpad: invert ");
- io_putstr(tp_invert_y ? "on" : "off");
- io_putchar('\n');
- return;
- }
- if (word_is(v, "on"))
- tp_invert_y = 1;
- else if (word_is(v, "off"))
- tp_invert_y = 0;
- else {
- io_putstr("touchpad: invert on|off\n");
- return;
- }
- io_putstr("touchpad: invert ");
- io_putstr(tp_invert_y ? "on" : "off");
- io_putchar('\n');
- arg = skip_spaces(v + word_len(v));
- } else {
- io_putstr("touchpad: usage: touchpad [probe | rightclick off|tap|corner|both | tapclick on|off | scroll on|off|natural|traditional | invert on|off]\n");
- return;
- }
- }
- }
|