Просмотр исходного кода

Add the Elan I2C touchpad on Braswell

The HP Chromebook 11 G5 EE has no PS/2 port; its touchpad is an Elan HID-over-I2C device on LPSS I2C6 (0xD1327000, slave 0x15). The LPSS I2C functions are hidden from PCI in ACPI mode, so i2c.c scans the firmware BAR windows for a DesignWare block and drives it polled in fast mode.

touchpad.c follows Linux's elan_i2c init order: reset, consume the 2-byte acknowledgement, read the HID descriptor, select absolute mode (0x0300) and wake the device (0x0800). The report line is a GPIO without a driver, so the input register is read every 25 ms (back-to-back reads stall the device). The Elan 0x5D vendor report provides the finger bitmask and five bytes per finger; absolute positions become PS/2 deltas and the clickpad bit becomes the left button. touchpad prints the probe result and mouseinfo lists the Elan VID/PID.
Johannes Zellner 1 неделя назад
Родитель
Сommit
b2f841ba0c
13 измененных файлов с 742 добавлено и 12 удалено
  1. 25 5
      AGENTS.md
  2. 9 3
      Makefile
  3. 32 1
      docs/drivers.html
  4. 1 1
      docs/index.html
  5. 252 0
      i2c.c
  6. 26 0
      i2c.h
  7. 17 2
      mouse.c
  8. 6 0
      sys_info.c
  9. 3 0
      syscall.h
  10. 2 0
      terminal.c
  11. 328 0
      touchpad.c
  12. 31 0
      touchpad.h
  13. 10 0
      ucmd_mouseinfo.c

+ 25 - 5
AGENTS.md

@@ -536,6 +536,20 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `usbhid.c/h` — USB HID boot devices: find + configure a mouse and/or
   `usbhid.c/h` — USB HID boot devices: find + configure a mouse and/or
   keyboard, arm their interrupt endpoints, decode mouse reports and inject
   keyboard, arm their interrupt endpoints, decode mouse reports and inject
   keyboard scancodes, watch for plug/unplug.
   keyboard scancodes, watch for plug/unplug.
+- **I2C touchpad**: `i2c.c/h` — polled Synopsys DesignWare APB I2C master
+  for the Braswell LPSS blocks (fast mode, component-type probe, explicit
+  LPSS reset-release so the FIFO drains); `touchpad.c/h` — Elan
+  HID-over-I2C touchpad on LPSS I2C6 at `0xD1327000`, slave `0x15`.
+  `touchpad_init()` runs from `mouse_init()` at boot (it maps the I2C
+  window before processes clone the page directory), resets the device,
+  consumes the 2-byte acknowledgement, reads the HID descriptor, selects
+  absolute mode (`0x0300 = 1`) and wakes it (`0x0800`).  The report line
+  is a GPIO with no driver, so `touchpad_poll()` reads the input register
+  every 25 ms (back-to-back reads stall the device), parses the Elan
+  `0x5D` vendor report (finger bitmask plus five bytes per finger) and
+  feeds absolute-to-relative deltas and the clickpad bit into the mouse
+  layer.  `mouseinfo` lists the Elan VID/PID and `touchpad` prints the
+  probe result.
 - **USB**: `usb.c/h` — controller-agnostic core: device enumeration,
 - **USB**: `usb.c/h` — controller-agnostic core: device enumeration,
   descriptor parsing, hub recursion, bus scans, per-controller addresses, the
   descriptor parsing, hub recursion, bus scans, per-controller addresses, the
   hotplug watcher and the interrupt-IN consumers (mouse + keyboard).  It keeps
   hotplug watcher and the interrupt-IN consumers (mouse + keyboard).  It keeps
@@ -851,7 +865,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 
 
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
-`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk),
+`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk),
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<ratio>]`, `spin [ms]`, `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), and `browser [url|file]` (disk app launcher).
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<ratio>]`, `spin [ms]`, `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), and `browser [url|file]` (disk app launcher).
 
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -1147,7 +1161,8 @@ so addresses do not collide.
   these devices.
   these devices.
 - **Debug**: `ohci:` / `uhci:` / `ehci:` / `usbhid:` lines go to COM1 (and
 - **Debug**: `ohci:` / `uhci:` / `ehci:` / `usbhid:` lines go to COM1 (and
   through the netlog mirror on serial-less boards); `mouseinfo` shows the
   through the netlog mirror on serial-less boards); `mouseinfo` shows the
-  active mouse backend and whether a USB keyboard is attached; `usb.c`'s
+  active mouse backend, the Elan VID/PID when the I2C touchpad is present,
+  and whether a USB keyboard is attached; `usb.c`'s
   `USB_DEBUG` and `usbhid.c`'s `USBHID_DEBUG` (both default 0) add
   `USB_DEBUG` and `usbhid.c`'s `USBHID_DEBUG` (both default 0) add
   scan/report tracing.  Headless tests: `make test-usb` runs
   scan/report tracing.  Headless tests: `make test-usb` runs
   `tools/qmp_cursor_test.py` (injects mouse motion with QMP
   `tools/qmp_cursor_test.py` (injects mouse motion with QMP
@@ -1404,9 +1419,6 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     errors even though the static page renders.  CSS also lacks
     errors even though the static page renders.  CSS also lacks
     border-radius clipping of descendant content, gradients/box-shadow/
     border-radius clipping of descendant content, gradients/box-shadow/
     transitions (a gradient background falls back to its last solid colour),
     transitions (a gradient background falls back to its last solid colour),
-    or CSS `color-mix`.  CSS custom properties (`var()`), flex rows with  Also no pseudo-classes/elements,
-    border-radius clipping of descendant content, gradients/box-shadow/
-    transitions (a gradient background falls back to its last solid colour),
     or CSS `color-mix`.  CSS custom properties (`var()`), flex rows with
     or CSS `color-mix`.  CSS custom properties (`var()`), flex rows with
     wrap/grow/gap, two-track and `repeat(auto-fit, minmax(...))` grids,
     wrap/grow/gap, two-track and `repeat(auto-fit, minmax(...))` grids,
     inline-block runs, basic proportional table columns and inline SVG
     inline-block runs, basic proportional table columns and inline SVG
@@ -1511,3 +1523,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     and some exact edge cases are not supported.  Float printf supports
     and some exact edge cases are not supported.  Float printf supports
     `%f/%e/%g` with a 900-digit precision cap.  These are sufficient for
     `%f/%e/%g` with a 900-digit precision cap.  These are sufficient for
     MuJS but are not a complete POSIX libc.
     MuJS but are not a complete POSIX libc.
+
+28. The Elan touchpad is polled at 40 Hz because its report GPIO (GPNC bank
+    index 18, level-low on setzer) has no driver.  Only finger 1 and the
+    clickpad bit are used: there is no multitouch, two-finger scrolling,
+    tap-to-click, drag-lock or pressure/size handling, and the 1/2 delta
+    scale is fixed (no pointer-acceleration curve).  The device stalls if
+    the input register is read back-to-back, so a GPIO interrupt (or a
+    larger minimum gap) is the prerequisite for a higher report rate.

+ 9 - 3
Makefile

@@ -271,7 +271,13 @@ fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
 
-mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h usbhid.h vga.h
+mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h touchpad.h usbhid.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+i2c.o: i2c.c i2c.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+touchpad.o: touchpad.c touchpad.h i2c.h io.h paging.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 idle.o: idle.c idle.h power.h
 idle.o: idle.c idle.h power.h
@@ -367,7 +373,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h pr
 syscall_stub.o: syscall_stub.S irqret.inc
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 	$(AS) $(ASFLAGS) -o $@ $<
 
 
-kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -1146,4 +1152,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 
 clean:
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan

+ 32 - 1
docs/drivers.html

@@ -44,7 +44,7 @@
 			<span class="pill">PCI enumeration</span>
 			<span class="pill">PCI enumeration</span>
 			<span class="pill">FDC · PATA · SATA</span>
 			<span class="pill">FDC · PATA · SATA</span>
 			<span class="pill">UHCI · OHCI · EHCI · xHCI</span>
 			<span class="pill">UHCI · OHCI · EHCI · xHCI</span>
-			<span class="pill">PS/2 + USB HID</span>
+			<span class="pill">PS/2 · USB · I2C touchpad</span>
 		</div>
 		</div>
 	</div>
 	</div>
 
 
@@ -84,6 +84,7 @@
 					<tr><td>USB</td><td><span class="file">usb.c</span> + <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span></td><td>PCI I/O ports or MMIO</td><td>Polled; hotplug watcher in the WM loop</td></tr>
 					<tr><td>USB</td><td><span class="file">usb.c</span> + <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span></td><td>PCI I/O ports or MMIO</td><td>Polled; hotplug watcher in the WM loop</td></tr>
 					<tr><td>Network</td><td><span class="file">e1000e.c</span> <span class="file">r8168.c</span> <span class="file">ne2000.c</span> <span class="file">asix.c</span></td><td>PCI or USB</td><td>Polled, one active receiver</td></tr>
 					<tr><td>Network</td><td><span class="file">e1000e.c</span> <span class="file">r8168.c</span> <span class="file">ne2000.c</span> <span class="file">asix.c</span></td><td>PCI or USB</td><td>Polled, one active receiver</td></tr>
 					<tr><td>PS/2</td><td><span class="file">mouse.c</span>, <span class="file">io.c</span></td><td>i8042</td><td>IRQ 1 keyboard, IRQ 12 mouse; polling fallback</td></tr>
 					<tr><td>PS/2</td><td><span class="file">mouse.c</span>, <span class="file">io.c</span></td><td>i8042</td><td>IRQ 1 keyboard, IRQ 12 mouse; polling fallback</td></tr>
+					<tr><td>Touchpad</td><td><span class="file">i2c.c</span>, <span class="file">touchpad.c</span></td><td>LPSS DesignWare I2C, slave <code>0x15</code></td><td>Polled input register every 25 ms; no GPIO interrupt driver</td></tr>
 					<tr><td>Serial</td><td><span class="file">serial.c</span></td><td>16550 UART, COM1</td><td>IRQ 4 ring buffer + polled transmitter</td></tr>
 					<tr><td>Serial</td><td><span class="file">serial.c</span></td><td>16550 UART, COM1</td><td>IRQ 4 ring buffer + polled transmitter</td></tr>
 					<tr><td>Thermal</td><td><span class="file">thermal.c</span></td><td>Intel DTS MSR</td><td>Sampled once a second from the scheduler tick</td></tr>
 					<tr><td>Thermal</td><td><span class="file">thermal.c</span></td><td>Intel DTS MSR</td><td>Sampled once a second from the scheduler tick</td></tr>
 					<tr><td>Board sensors</td><td><span class="file">it87.c</span>, <span class="file">tpec.c</span></td><td>Super I/O, ThinkPad EC (temps, fan, battery, AC)</td><td>Polled; only the EC page selector is written, then restored</td></tr>
 					<tr><td>Board sensors</td><td><span class="file">it87.c</span>, <span class="file">tpec.c</span></td><td>Super I/O, ThinkPad EC (temps, fan, battery, AC)</td><td>Polled; only the EC page selector is written, then restored</td></tr>
@@ -295,6 +296,34 @@
 			keyboard is attached. Headless regression tests inject mouse motion
 			keyboard is attached. Headless regression tests inject mouse motion
 			and keys through QMP and verify the result with screen dumps.
 			and keys through QMP and verify the result with screen dumps.
 		</p>
 		</p>
+
+		<h3>I2C touchpad</h3>
+		<p>
+			The HP Chromebook 11 G5 EE has no PS/2 port: its Elan touchpad is a
+			HID-over-I2C device on LPSS I2C6. The LPSS I2C functions are hidden
+			from PCI in ACPI mode, so <span class="file">touchpad.c</span> scans
+			the two firmware BAR windows for a DesignWare component and
+			<span class="file">i2c.c</span> drives that controller in fast mode,
+			polled and without interrupts.
+		</p>
+		<ul>
+			<li><strong>Probe.</strong> Slave address <code>0x15</code>: reset
+			(<code>0x0100</code>), consume the 2-byte acknowledgement, read the
+			HID descriptor, select absolute mode (<code>0x0300 = 1</code>) and
+			wake the device (<code>0x0800</code>). Resetting last leaves it
+			silent until the next power cycle.</li>
+			<li><strong>Reports.</strong> The Elan vendor report <code>0x5D</code>
+			carries a finger bitmask and five bytes per finger (X/Y high bits, X
+			low, Y low, size, pressure). Successive absolute positions become
+			PS/2 deltas; the clickpad bit becomes the left button.</li>
+			<li><strong>Polling.</strong> The report GPIO has no driver yet, so
+			the input register is read every 25 ms. Back-to-back reads stall the
+			device, which is why the poll is throttled rather than run flat
+			out.</li>
+			<li><strong>Discovery.</strong> <code>touchpad</code> prints the
+			probe result; <code>mouseinfo</code> lists the Elan vendor/product
+			ID next to the PS/2 and USB backends.</li>
+		</ul>
 	</section>
 	</section>
 
 
 	<section id="serial">
 	<section id="serial">
@@ -400,6 +429,8 @@
 			<li><code>usb.c</code>, <code>usb_hcd.h</code> — USB core</li>
 			<li><code>usb.c</code>, <code>usb_hcd.h</code> — USB core</li>
 			<li><code>uhci.c</code>, <code>ohci.c</code>, <code>ehci.c</code>, <code>xhci.c</code></li>
 			<li><code>uhci.c</code>, <code>ohci.c</code>, <code>ehci.c</code>, <code>xhci.c</code></li>
 			<li><code>usbhid.c</code>, <code>mouse.c</code>, <code>io.c</code> — input</li>
 			<li><code>usbhid.c</code>, <code>mouse.c</code>, <code>io.c</code> — input</li>
+			<li><code>i2c.c</code>, <code>i2c.h</code> — DesignWare/LPSS I2C master</li>
+			<li><code>touchpad.c</code>, <code>touchpad.h</code> — Elan HID-over-I2C touchpad</li>
 			<li><code>serial.c</code>, <code>serial_ring_buf.c</code></li>
 			<li><code>serial.c</code>, <code>serial_ring_buf.c</code></li>
 			<li><code>thermal.c</code>, <code>it87.c</code>, <code>tpec.c</code>, <code>mtrr.c</code></li>
 			<li><code>thermal.c</code>, <code>it87.c</code>, <code>tpec.c</code>, <code>mtrr.c</code></li>
 			<li><code>rtc.c</code> — CMOS wall clock read/write</li>
 			<li><code>rtc.c</code> — CMOS wall clock read/write</li>

+ 1 - 1
docs/index.html

@@ -202,7 +202,7 @@
 			</a>
 			</a>
 			<a class="card" href="drivers.html">
 			<a class="card" href="drivers.html">
 				<h3>Drivers &amp; buses</h3>
 				<h3>Drivers &amp; buses</h3>
-				<p>PCI enumeration, FDC/PATA/SATA, the USB core and four host controllers, HID, serial and board sensors.</p>
+				<p>PCI enumeration, FDC/PATA/SATA, the USB core and four host controllers, HID, the I2C touchpad, serial and board sensors.</p>
 				<span class="card-tag">drivers.html</span>
 				<span class="card-tag">drivers.html</span>
 			</a>
 			</a>
 			<a class="card" href="filesystems.html">
 			<a class="card" href="filesystems.html">

+ 252 - 0
i2c.c

@@ -0,0 +1,252 @@
+/* Polled Synopsys DesignWare APB I2C master (Braswell LPSS).
+ *
+ * Only master transfers are implemented.  The bus runs in fast mode (the
+ * SCL counts below give 300-400 kHz depending on the 100/133 MHz LPSS
+ * clock), which is within the touchpad's 400 kHz rating and keeps a 34-byte
+ * report read short.
+ *
+ * Register offsets follow the DesignWare databook; the component type at
+ * 0xFC is the usual "DW" signature (0x44570140).  The Intel LPSS private
+ * block at 0x800 holds the reset release register; firmware has already
+ * released the controller, but the I2C FSM only drains its FIFOs after an
+ * explicit assert/release cycle, so i2c_dw_init() always does one. */
+
+#include "i2c.h"
+#include "timer.h"
+
+#define DW_IC_CON		0x00
+#define DW_IC_TAR		0x04
+#define DW_IC_DATA_CMD		0x10
+#define DW_IC_SS_SCL_HCNT	0x14
+#define DW_IC_SS_SCL_LCNT	0x18
+#define DW_IC_FS_SCL_HCNT	0x1c
+#define DW_IC_FS_SCL_LCNT	0x20
+#define DW_IC_RAW_INTR_STAT	0x34
+#define DW_IC_RX_TL		0x38
+#define DW_IC_TX_TL		0x3c
+#define DW_IC_CLR_INTR		0x40
+#define DW_IC_CLR_TX_ABRT	0x54
+#define DW_IC_ENABLE		0x6c
+#define DW_IC_STATUS		0x70
+#define DW_IC_TX_ABRT_SOURCE	0x80
+#define DW_IC_TXFLR		0x74
+#define DW_IC_RXFLR		0x78
+#define DW_IC_ENABLE_STATUS	0x9c
+#define DW_IC_FS_SPKLEN		0xa0
+#define DW_IC_COMP_VERSION	0xf8
+#define DW_IC_COMP_TYPE		0xfc
+#define DW_IC_COMP_TYPE_VALUE	0x44570140u
+#define DW_LPSS_RESET		0x804
+
+#define IC_CON_MASTER		0x00000001u
+#define IC_CON_SPEED_FS		0x00000004u
+#define IC_CON_RESTART_EN	0x00000020u
+#define IC_CON_SLAVE_DISABLE	0x00000040u
+
+#define IC_DATA_CMD_READ	0x00000100u
+#define IC_DATA_CMD_STOP	0x00000200u
+#define IC_DATA_CMD_RESTART	0x00000400u
+
+#define IC_STATUS_TFNF		0x00000002u
+#define IC_STATUS_RFNE		0x00000004u
+#define IC_STATUS_TFE		0x00000004u
+#define IC_STATUS_MST_ACTIVITY	0x00000020u
+
+#define IC_ENABLE_EN		0x00000001u
+#define IC_ENABLE_ABORT		0x00000002u
+
+#define IC_RAW_TX_ABRT		0x00000040u
+
+unsigned int i2c_last_abort;
+
+static unsigned int dw_rd(unsigned int base, unsigned int off)
+{
+	return *(volatile unsigned int *)(base + off);
+}
+
+static void dw_wr(unsigned int base, unsigned int off, unsigned int val)
+{
+	*(volatile unsigned int *)(base + off) = val;
+}
+
+static void dw_delay_ms(unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
+
+/* Wait until (status & mask) == want.  A bus abort ends the wait early so a
+ * missing device cannot burn the whole timeout. */
+static int dw_wait(unsigned int base, unsigned int off, unsigned int mask,
+		   unsigned int want, unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (get_ticks_ms() - t0 < ms) {
+		if ((dw_rd(base, off) & mask) == want)
+			return 0;
+		if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
+			return -1;
+	}
+	return -1;
+}
+
+static void dw_clear_irq(unsigned int base)
+{
+	(void)dw_rd(base, DW_IC_CLR_TX_ABRT);
+	(void)dw_rd(base, DW_IC_CLR_INTR);
+}
+
+/* Stop anything in flight and put the controller back in the idle state. */
+static void dw_recover(unsigned int base)
+{
+	dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN | IC_ENABLE_ABORT);
+	dw_delay_ms(1);
+	dw_wr(base, DW_IC_ENABLE, 0);
+	dw_clear_irq(base);
+	dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
+}
+
+unsigned int i2c_dw_comp_type(unsigned int base)
+{
+	return dw_rd(base, DW_IC_COMP_TYPE);
+}
+
+void i2c_dw_reset_release(unsigned int base)
+{
+	/* Bay Trail/Braswell put the LPSS private block at 0x800 (reset at
+	 * 0x804); newer LPSS layouts put it at 0x200 (reset at 0x204).  The
+	 * Linux intel-lpss driver asserts the reset first and then releases
+	 * it, so do both here: a block left in functional reset keeps the
+	 * register file alive but never clocks the I2C master FSM, which
+	 * looks exactly like "commands sit in the TX FIFO forever". */
+	dw_wr(base, 0x804, 0);
+	dw_wr(base, 0x204, 0);
+	dw_delay_ms(1);
+	dw_wr(base, 0x804, 3);
+	dw_wr(base, 0x204, 3);
+	dw_delay_ms(1);
+}
+
+int i2c_dw_present(unsigned int base)
+{
+	return i2c_dw_comp_type(base) == DW_IC_COMP_TYPE_VALUE;
+}
+
+int i2c_dw_init(unsigned int base)
+{
+	unsigned int t0;
+
+	if (!i2c_dw_present(base))
+		return -1;
+
+	/* Release the LPSS block from reset. */
+	i2c_dw_reset_release(base);
+
+	dw_wr(base, DW_IC_ENABLE, 0);
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < 20) {
+		if (!(dw_rd(base, DW_IC_ENABLE_STATUS) & 1))
+			break;
+	}
+
+	dw_wr(base, DW_IC_CON, IC_CON_MASTER | IC_CON_SPEED_FS |
+	       IC_CON_RESTART_EN | IC_CON_SLAVE_DISABLE);
+	/* 400 kHz at 133 MHz; 300 kHz at 100 MHz.  The LPSS clock is one
+	 * of those, so the bus never exceeds the touchpad's 400 kHz. */
+	dw_wr(base, DW_IC_FS_SCL_HCNT, 112);
+	dw_wr(base, DW_IC_FS_SCL_LCNT, 211);
+	dw_wr(base, DW_IC_FS_SPKLEN, 1);
+	dw_wr(base, DW_IC_RX_TL, 0);
+	dw_wr(base, DW_IC_TX_TL, 0);
+	dw_clear_irq(base);
+
+	dw_wr(base, DW_IC_ENABLE, IC_ENABLE_EN);
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < 20) {
+		if (dw_rd(base, DW_IC_ENABLE_STATUS) & 1)
+			return 0;
+	}
+	return -1;
+}
+
+int i2c_dw_transfer(unsigned int base, unsigned int addr,
+		    const unsigned char *wr, unsigned int wrlen,
+		    unsigned char *rd, unsigned int rdlen)
+{
+	unsigned int i, cmd, t0, deadline;
+
+	if (!wrlen && !rdlen)
+		return 0;
+
+	/* Worst case at 300 kHz, plus slack for clock stretching.  Keep it
+	 * short: a failed poll must not block the housekeeping loop. */
+	deadline = 10 + (wrlen + rdlen);
+
+	dw_wr(base, DW_IC_TAR, addr & 0x7f);
+	dw_clear_irq(base);
+	i2c_last_abort = 0;
+
+	for (i = 0; i < wrlen; i++) {
+		if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
+			    IC_STATUS_TFNF, deadline))
+			goto fail;
+		cmd = wr[i];
+		if (i + 1 == wrlen && !rdlen)
+			cmd |= IC_DATA_CMD_STOP;
+		dw_wr(base, DW_IC_DATA_CMD, cmd);
+	}
+
+	for (i = 0; i < rdlen; i++) {
+		if (dw_wait(base, DW_IC_STATUS, IC_STATUS_TFNF,
+			    IC_STATUS_TFNF, deadline))
+			goto fail;
+		cmd = IC_DATA_CMD_READ;
+		if (i == 0 && wrlen)
+			cmd |= IC_DATA_CMD_RESTART;
+		if (i + 1 == rdlen)
+			cmd |= IC_DATA_CMD_STOP;
+		dw_wr(base, DW_IC_DATA_CMD, cmd);
+		if (dw_wait(base, DW_IC_STATUS, IC_STATUS_RFNE,
+			    IC_STATUS_RFNE, deadline))
+			goto fail;
+		rd[i] = (unsigned char)(dw_rd(base, DW_IC_DATA_CMD) & 0xff);
+	}
+
+	if (!rdlen) {
+		/* A write-only transfer is done when the controller has
+		 * consumed every command (TX FIFO empty).  MST_ACTIVITY is
+		 * already low before the transfer starts, so checking it
+		 * alone would report a hung FIFO as success. */
+		t0 = get_ticks_ms();
+		while (get_ticks_ms() - t0 < deadline) {
+			if (dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE)
+				break;
+			if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
+				goto fail;
+		}
+		if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_TFE))
+			goto fail;
+		dw_clear_irq(base);
+		return 0;
+	}
+
+	/* Wait for the STOP to finish. */
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < deadline) {
+		if (!(dw_rd(base, DW_IC_STATUS) & IC_STATUS_MST_ACTIVITY))
+			break;
+	}
+	if (dw_rd(base, DW_IC_RAW_INTR_STAT) & IC_RAW_TX_ABRT)
+		goto fail;
+
+	dw_clear_irq(base);
+	return 0;
+
+fail:
+	i2c_last_abort = dw_rd(base, DW_IC_TX_ABRT_SOURCE);
+	dw_recover(base);
+	return -1;
+}

+ 26 - 0
i2c.h

@@ -0,0 +1,26 @@
+/* Polled Synopsys DesignWare APB I2C master.  Used by the Braswell LPSS
+ * controllers, in particular the one the HID-over-I2C touchpad sits on. */
+#ifndef I2C_H
+#define I2C_H
+
+/* Probe and initialize the controller at a physical MMIO base.  Returns 0
+ * when the DesignWare component type matches. */
+int i2c_dw_init(unsigned int base);
+
+/* True when the controller at base looks like a DesignWare I2C block. */
+int i2c_dw_present(unsigned int base);
+
+/* TX_ABRT_SOURCE of the last failed transfer (0 = timeout, not a NAK). */
+extern unsigned int i2c_last_abort;
+
+/* Raw component-type / reset-release access for the LPSS scan. */
+unsigned int i2c_dw_comp_type(unsigned int base);
+void i2c_dw_reset_release(unsigned int base);
+
+/* Combined write + repeated-start read, the form HID-over-I2C uses for
+ * register-addressed transfers.  Either buffer may be empty. */
+int i2c_dw_transfer(unsigned int base, unsigned int addr,
+		    const unsigned char *wr, unsigned int wrlen,
+		    unsigned char *rd, unsigned int rdlen);
+
+#endif

+ 17 - 2
mouse.c

@@ -5,6 +5,7 @@
 #include "intel.h"
 #include "intel.h"
 #include "radeon.h"
 #include "radeon.h"
 #include "timer.h"
 #include "timer.h"
+#include "touchpad.h"
 #include "usbhid.h"
 #include "usbhid.h"
 #include "vga.h"
 #include "vga.h"
 
 
@@ -343,6 +344,11 @@ void mouse_init(void)
 	mouse_aux_irq_ok = 0;
 	mouse_aux_irq_ok = 0;
 	pkt_idx = 0;
 	pkt_idx = 0;
 
 
+	/* Built-in HID-over-I2C touchpad.  The probe maps the LPSS I2C
+	 * window, so it must run at boot, before the kworker and command
+	 * processes clone the page directory. */
+	touchpad_init();
+
 	enable_aux_port();
 	enable_aux_port();
 	mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
 	mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
 
 
@@ -474,10 +480,12 @@ void mouse_poll_update(void)
 	unsigned int guard = 0;
 	unsigned int guard = 0;
 	unsigned int usb_buttons = 0;
 	unsigned int usb_buttons = 0;
 	int usb_dx = 0, usb_dy = 0;
 	int usb_dx = 0, usb_dy = 0;
+	int tp_buttons = 0, tp_dx = 0, tp_dy = 0;
 
 
 	/* The cursor is set up for whichever backend shows up first (PS/2
 	/* The cursor is set up for whichever backend shows up first (PS/2
 	   is probed at boot, USB may attach later). */
 	   is probed at boot, USB may attach later). */
-	if ((mouse_present_flag || usbhid_mouse_attached()) && !mouse_cursor_flag)
+	if ((mouse_present_flag || usbhid_mouse_attached() ||
+	     touchpad_present()) && !mouse_cursor_flag)
 		mouse_cursor_setup();
 		mouse_cursor_setup();
 
 
 	/* USB HID mouse: reports arrive through the armed interrupt
 	/* USB HID mouse: reports arrive through the armed interrupt
@@ -487,6 +495,12 @@ void mouse_poll_update(void)
 		mouse_move(usb_dx, usb_dy);
 		mouse_move(usb_dx, usb_dy);
 	}
 	}
 
 
+	/* Built-in touchpad: absolute positions converted to deltas. */
+	if (touchpad_poll(&tp_buttons, &tp_dx, &tp_dy)) {
+		mouse_buttons = tp_buttons;
+		mouse_move(tp_dx, tp_dy);
+	}
+
 	if (mouse_irq_mode)
 	if (mouse_irq_mode)
 		return;                 /* IRQ12 owns the PS/2 packets */
 		return;                 /* IRQ12 owns the PS/2 packets */
 	if (!mouse_streaming_flag)
 	if (!mouse_streaming_flag)
@@ -515,7 +529,8 @@ void mouse_poll_update(void)
 
 
 int mouse_is_present(void)
 int mouse_is_present(void)
 {
 {
-	return mouse_present_flag || usbhid_mouse_attached();
+	return mouse_present_flag || usbhid_mouse_attached() ||
+	       touchpad_present();
 }
 }
 
 
 int mouse_ps2_present(void)
 int mouse_ps2_present(void)

+ 6 - 0
sys_info.c

@@ -11,6 +11,7 @@
 #include "sched.h"
 #include "sched.h"
 #include "sys_fs.h"
 #include "sys_fs.h"
 #include "thermal.h"
 #include "thermal.h"
+#include "touchpad.h"
 #include "tpec.h"
 #include "tpec.h"
 #include "usb.h"
 #include "usb.h"
 #include "usbhid.h"
 #include "usbhid.h"
@@ -85,6 +86,11 @@ int sys_mouseinfo(struct sys_mouseinfo *out)
 			 sizeof(out->hcd));
 			 sizeof(out->hcd));
 	}
 	}
 	out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
 	out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
+	out->touchpad = touchpad_present() ? 1 : 0;
+	if (out->touchpad) {
+		out->tp_vendor = touchpad_vendor();
+		out->tp_product = touchpad_product();
+	}
 	return 1;
 	return 1;
 }
 }
 
 

+ 3 - 0
syscall.h

@@ -357,6 +357,9 @@ struct sys_mouseinfo {
 	unsigned int cursor_visible;
 	unsigned int cursor_visible;
 	char hcd[8];
 	char hcd[8];
 	char backend[16];
 	char backend[16];
+	unsigned int touchpad;
+	unsigned int tp_vendor;
+	unsigned int tp_product;
 };
 };
 
 
 struct sys_storage_info {
 struct sys_storage_info {

+ 2 - 0
terminal.c

@@ -20,6 +20,7 @@ extern void program_display_main(void);
 extern void program_gfxbench_main(void);
 extern void program_gfxbench_main(void);
 extern void program_serial_loop_main(const char *arg);
 extern void program_serial_loop_main(const char *arg);
 extern void program_lsusb_main(void);
 extern void program_lsusb_main(void);
+extern void program_touchpad_main(void);
 extern void program_netdev_main(const char *arg);
 extern void program_netdev_main(const char *arg);
 extern void program_format_main(const char *arg);
 extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_lspci_main(const char *filter);
@@ -82,6 +83,7 @@ static const struct kcmd cmds[] = {
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_main, 0, 0, 0 },
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_main, 0, 0, 0 },
 	{ "gfxbench",   "gfxbench",                      CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
 	{ "gfxbench",   "gfxbench",                      CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
 	{ "lsusb",      "lsusb",                         CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
 	{ "lsusb",      "lsusb",                         CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
+	{ "touchpad",   "touchpad",                      CK_SPAWN, 0, program_touchpad_main, 0, 0, 0 },
 	{ "netdev",     "netdev [name]",                 CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
 	{ "netdev",     "netdev [name]",                 CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
 	{ "format",     "format <device> [size]",        CK_CUSTOM, 0, 0, 0, 0, cmd_format },
 	{ "format",     "format <device> [size]",        CK_CUSTOM, 0, 0, 0, 0, cmd_format },
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },

+ 328 - 0
touchpad.c

@@ -0,0 +1,328 @@
+/* Elan HID-over-I2C touchpad on the Braswell LPSS I2C bus.
+ *
+ * The HP Chromebook 11 G5 EE (setzer) wires its touchpad to LPSS I2C6 at
+ * 0xD1327000, slave address 0x15.  The LPSS I2C functions are hidden from
+ * PCI in ACPI mode, so the driver scans the two firmware BAR windows for
+ * DesignWare component types instead of enumerating PCI.
+ *
+ * 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 Linux's elan_i2c: reset, consume the 2-byte reset
+ * acknowledgement, read the HID descriptor, select absolute mode (0x0300)
+ * and wake the device (0x0800).  Resetting last leaves it silent until the
+ * next power cycle.
+ */
+
+#include "touchpad.h"
+#include "i2c.h"
+#include "io.h"
+#include "paging.h"
+#include "timer.h"
+
+#define TP_ADDR			0x15
+#define TP_CTRL_MAX		16
+#define TP_CTRL_STEP		0x1000u
+
+#define TP_HID_DESC_REG		0x0001
+#define TP_HID_DESC_LEN		30
+
+#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_MAX_INPUT		64
+#define TP_POLL_MS		25
+
+static unsigned int tp_base;
+static unsigned int tp_addr = TP_ADDR;
+static int tp_present;
+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 const char *tp_err;
+static unsigned int tp_err_val;
+
+static int tp_last_x, tp_last_y, tp_acc_x, tp_acc_y;
+static int tp_down, tp_last_buttons;
+static unsigned int tp_next_poll;
+
+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: 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_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 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);
+}
+
+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);
+
+	tp_present = 1;
+	tp_err = 0;
+	print_summary();
+	return 1;
+}
+
+int touchpad_init(void)
+{
+	/* The ACPI _CRS address for the 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;
+
+	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 = ranges[r] + i * TP_CTRL_STEP;
+
+			paging_map_mmio(base, 0x1000);
+			/* 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;
+		}
+	}
+	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;
+}
+
+int touchpad_poll(int *buttons, int *dx, int *dy)
+{
+	unsigned char buf[TP_MAX_INPUT];
+	unsigned int now, b;
+
+	if (!tp_present)
+		return 0;
+
+	now = get_ticks_ms();
+	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;
+
+	/* The addressed read returns two status bytes, the 2-byte length
+	 * and then the report, so locate the 0x5D report ID instead of
+	 * trusting a fixed offset.  Layout after it (Linux hid-elan.c):
+	 * touch info (f5..f1 at bits 7..3, clickpad at bit 0), then 5
+	 * bytes per finger: x11:8/y11:9, x7:0, y7:0, size, pressure. */
+	for (b = 0; b < 6 && b + 4 < tp_max_in; b++)
+		if (buf[b] == TP_REPORT_ID)
+			break;
+	if (b >= 6 || b + 4 >= tp_max_in)
+		return 0;
+
+	{
+		unsigned int ti = buf[b + 1];
+		unsigned int xh = buf[b + 2];
+		int x = ((int)(xh & 0xf0) << 4) | buf[b + 3];
+		int y = ((int)(xh & 0x0f) << 8) | buf[b + 4];
+
+		*buttons = ti & 0x01;
+		if (!(ti & 0x08)) {		/* finger 1 lifted */
+			tp_down = 0;
+			tp_acc_x = 0;
+			tp_acc_y = 0;
+			if (tp_last_buttons != *buttons) {
+				tp_last_buttons = *buttons;
+				return 1;
+			}
+			return 0;
+		}
+		if (!tp_down) {
+			tp_down = 1;
+			tp_last_x = x;
+			tp_last_y = y;
+			*dx = 0;
+			*dy = 0;
+		} else {
+			tp_acc_x += x - tp_last_x;
+			/* The Elan's Y axis increases upwards, already the
+			 * PS/2 convention: dragging up gives a positive dy. */
+			tp_acc_y += y - tp_last_y;
+			tp_last_x = x;
+			tp_last_y = y;
+			*dx = tp_acc_x / 2;
+			tp_acc_x -= *dx * 2;
+			*dy = tp_acc_y / 2;
+			tp_acc_y -= *dy * 2;
+		}
+		if (*dx > 64)
+			*dx = 64;
+		if (*dx < -64)
+			*dx = -64;
+		if (*dy > 64)
+			*dy = 64;
+		if (*dy < -64)
+			*dy = -64;
+		if (tp_last_buttons != *buttons) {
+			tp_last_buttons = *buttons;
+			return 1;
+		}
+		return *dx || *dy;
+	}
+}
+
+void program_touchpad_main(void)
+{
+	if (!tp_present) {
+		io_putstr("touchpad: absent (");
+		io_putstr(tp_err ? tp_err : "unknown");
+		if (tp_err_val) {
+			io_putstr(" ab=");
+			io_puthex(tp_err_val);
+		}
+		io_putstr(")\n");
+		return;
+	}
+	print_summary();
+}

+ 31 - 0
touchpad.h

@@ -0,0 +1,31 @@
+/* Elan HID-over-I2C touchpad (HP Chromebook 11 G5 EE / setzer).
+ *
+ * The controller is a DesignWare I2C block in the Braswell LPSS; the
+ * touchpad answers at slave address 0x15 with the HID-over-I2C register
+ * protocol (16-bit little-endian register addresses).  The device reports
+ * absolute finger positions in the Elan 0x5D vendor report; the driver turns
+ * successive positions into pointer deltas.
+ */
+#ifndef TOUCHPAD_H
+#define TOUCHPAD_H
+
+/* Probe the LPSS I2C controllers and bring the touchpad up.  Returns 1 when
+ * a device was found.  Must run at boot, before processes clone the kernel
+ * page directory (it maps the I2C MMIO window). */
+int touchpad_init(void);
+
+int touchpad_present(void);
+
+/* Identification of the probed device (valid when present). */
+unsigned int touchpad_addr(void);
+unsigned short touchpad_vendor(void);
+unsigned short touchpad_product(void);
+
+/* `touchpad` kernel command: print the cached probe result. */
+void program_touchpad_main(void);
+
+/* Poll one input report.  Returns 1 when the deltas or buttons changed;
+ * values use the mouse layer's PS/2 convention (+dy is up). */
+int touchpad_poll(int *buttons, int *dx, int *dy);
+
+#endif

+ 10 - 0
ucmd_mouseinfo.c

@@ -55,6 +55,16 @@ void _start(const char *arg)
 		u_puts(m.usb_low ? " (low speed)\n" : " (full speed)\n");
 		u_puts(m.usb_low ? " (low speed)\n" : " (full speed)\n");
 	}
 	}
 
 
+	if (m.touchpad) {
+		u_puts("mouse touchpad elan ");
+		u_put_hex2(m.tp_vendor >> 8);
+		u_put_hex2(m.tp_vendor);
+		u_putc(':');
+		u_put_hex2(m.tp_product >> 8);
+		u_put_hex2(m.tp_product);
+		u_putc('\n');
+	}
+
 	u_puts("mouse usb keyboard ");
 	u_puts("mouse usb keyboard ");
 	u_puts(m.usb_keyboard ? "yes\n" : "no\n");
 	u_puts(m.usb_keyboard ? "yes\n" : "no\n");
 	u_puts("mouse cursor ");
 	u_puts("mouse cursor ");