Browse Source

Show the terminal before probing USB, the NIC and disks

The WM spawns the first terminal as soon as /ramfs is mounted; kernel_main now publishes vfs_scan_busy, calls wm_server_boot_ready(), waits (hlt, 1 s cap) for the first frame, and only then brings up USB, the NIC and the bootscan storage thread.  On QEMU's USB image the terminal is on screen ~0.35-0.4 s after the kernel starts instead of waiting for those probes.

- boot.S: CHS reads a run up to the end of the track and never across a 64 KB DMA boundary, EDD up to 64 sectors; a failing run gets three tries, then falls back to one sector per call.
- kernel.c: defer USB/NIC/storage until the terminal presents, publish the scan before the WM starts, and keep the USB bring-up non-preemptible; sys_wm.c/h publishes wm_first_window_presented.
- sys_net.c: SYS_NET_INFO from the WM reports no NIC until kernel_main has probed, so the first top-bar poll cannot delay the first frame.
- usb.c/usbhid.c: claim the scan before usb_hcd_init(), so a USB call from the running desktop sees usb_scan_foreign rather than half-initialized controllers; usbhid_init() uses usb_scan().
- touchpad.c: probe the fixed LPSS windows only on Airmont (family 6 model 0x4C), skipping ~64 ms of reset cycles elsewhere; cpu.c/h adds cpu_family_model().
- AGENTS.md and docs/boot, drivers and development updated.
Johannes Zellner 1 tuần trước cách đây
mục cha
commit
6f2a56df99
14 tập tin đã thay đổi với 206 bổ sung và 83 xóa
  1. 25 12
      AGENTS.md
  2. 42 21
      boot.S
  3. 21 0
      cpu.c
  4. 5 0
      cpu.h
  5. 17 9
      docs/boot.html
  6. 1 1
      docs/development.html
  7. 3 1
      docs/drivers.html
  8. 54 34
      kernel.c
  9. 7 0
      sys_net.c
  10. 3 0
      sys_wm.c
  11. 4 0
      sys_wm.h
  12. 10 0
      touchpad.c
  13. 8 3
      usb.c
  14. 6 2
      usbhid.c

+ 25 - 12
AGENTS.md

@@ -142,9 +142,13 @@ make clean
 ## Boot chain
 
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
-   to `0x7E00` (CHS for floppy, one sector per call; INT 13h AH=42h LBA for
-   hard disk, up to 64 sectors per call into `ES:0` with `ES` advanced, so the
-   offset never wraps), jumps there with the boot drive still in DL.
+   to `0x7E00`, one INT 13h per run of sectors into `ES:0` with `ES`
+   advanced (the offset never wraps): EDD LBA for hard disks, up to 64
+   sectors; CHS for floppies, up to the end of the track and never across a
+   64 KB physical ISA DMA boundary.  Errors get three tries per run, then one
+   sector per call (the old behaviour), three tries, then `Disk read error`.
+   Jumps to the stage with the boot drive still in DL (do not clobber DX).
+   The 512-byte sector has only a few bytes left before the partition table.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
    enables A20, reads the `NBROOT1` rootfs header at the boot sector's
    `pack_lba` field and copies the gzip pack directly to `PACK_HIGH_ADDR`
@@ -170,14 +174,20 @@ make clean
    used); fixed disks use EDD LBA.
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → power → idle → processes → timer (PIT; 1 kHz busy,
-   100 Hz idle + IDT) → SMP AP bring-up → mouse/USB → VGA shadow buffer →
-   ramfs inflate → WM → NIC probe → kworker → the `bootscan` kernel thread
-   (storage scan + FAT mounts, then the `/data` WiFi autoconnect once the
-   boot DHCP finished) → `wm_server_boot_ready()` immediately, so the first
-   terminal starts while the disks are still probed → DHCP.  COM1 prints
-   `wm: first window frame (term) at N ms` (uptime) when the first app
-   window presents: the end point of a boot-time measurement (~0.65 s after
-   the kernel starts on QEMU's USB image).
+   100 Hz idle + IDT) → SMP AP bring-up → PS/2 mouse (the I2C touchpad
+   probe only on Airmont) → VGA shadow buffer → ramfs inflate → WM →
+   publish `vfs_scan_busy` → `wm_server_boot_ready()` → wait (idle `hlt`,
+   1 s cap) until the first terminal has presented → USB bring-up and scan
+   (non-preemptible, as before the WM existed: reset/SOF timing) → NIC probe
+   → kworker → the `bootscan` kernel thread (storage scan + FAT mounts, then
+   the `/data` WiFi autoconnect once the boot DHCP finished) → DHCP.  So
+   the terminal is on screen before USB, NIC and disks are probed; USB
+   input, USB NICs and USB disks attach right after.  The WM's
+   `SYS_NET_INFO` does not trigger the NIC probe (it reports "no NIC" until
+   `kernel_main` probed), so the first frame never waits for a PHY reset.
+   COM1 prints `wm: first window frame (term) at N ms` (uptime) when the
+   first app window presents: the end point of a boot-time measurement
+   (~0.35-0.4 s after the kernel starts on QEMU's USB image).
 4. `mbr.S` — MBR (USB/HDD boot); loads the active partition's boot sector
    (partition 1 at LBA 63).
 5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
@@ -854,7 +864,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   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
+  `touchpad_init()` runs from `mouse_init()` at boot, only when
+  `cpu_family_model()` is Airmont `0x64C` (elsewhere the fixed-address LPSS
+  scan would reset-cycle 32 MMIO windows that belong to nobody, ~64 ms, or
+  to another device); 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`), wakes it (`0x0800`) and then queries the

+ 42 - 21
boot.S

@@ -69,24 +69,27 @@ load_kernel:
 	movw  $0x07e0, %ax
 	movw  %ax, %es
 
-	/* EDD reads up to 64 sectors per INT 13h into ES:0 and then advances
-	 * ES, so the offset never wraps (one call per sector made the kernel
-	 * load ~700 BIOS calls).  CHS (floppy) stays at one sector: a longer
-	 * transfer could cross a 64 KB ISA DMA boundary. */
+	/* One INT 13h per run of sectors into ES:0, then ES advances, so the
+	 * offset never wraps.  EDD reads up to 64 sectors; CHS (floppy) reads
+	 * up to the end of the track and never across a 64 KB physical ISA
+	 * DMA boundary (the buffer starts 512-byte aligned at 0x7E00).  One
+	 * call per sector made the kernel load ~700 BIOS calls, each of which
+	 * can cost a full floppy revolution.  Errors: three tries per run,
+	 * then one sector per call (the old behaviour), three tries again,
+	 * then "Disk read error". */
 load_loop:
 	testw %cx, %cx
 	jz    load_done
 	pushw %cx
 	pushw %si
-	movw  $1, %ax
-	movb  boot_drive, %dl
-	cmpb  $0x80, %dl
-	jb    read_chs
 	movw  %cx, %ax
 	cmpw  chunk_max, %ax
 	jbe   1f
 	movw  chunk_max, %ax
-1:	movw  %ax, cur_count
+1:	movb  boot_drive, %dl
+	cmpb  $0x80, %dl
+	jb    read_chs
+	movw  %ax, cur_count
 	movw  %ax, 0x502
 	xorw  %ax, %ax
 	movw  %ax, 0x504
@@ -101,11 +104,24 @@ load_loop:
 	movb  $0x42, %ah
 	jmp   do_int13
 read_chs:
-	movw  %ax, cur_count
-	movw  %si, %ax
+	movw  %ax, %bx            /* bx = sectors to read */
+	movw  %es, %ax
+	shlw  $4, %ax             /* low 16 bits of the physical address */
+	negw  %ax                 /* bytes to the next 64 KB boundary */
+	shrw  $9, %ax
+	jz    3f                  /* 0: a whole 64 KB window is free */
+	cmpw  %ax, %bx
+	jbe   3f
+	movw  %ax, %bx
+3:	movw  %si, %ax
 	xorw  %dx, %dx
 	movw  $18, %di
-	divw  %di
+	divw  %di                 /* ax = track index, dx = sector - 1 */
+	subw  %dx, %di            /* sectors left on this track */
+	cmpw  %di, %bx
+	jbe   4f
+	movw  %di, %bx
+4:	movw  %bx, cur_count
 	movb  %dl, %cl
 	incb  %cl
 	xorw  %dx, %dx
@@ -115,23 +131,26 @@ read_chs:
 	movb  %al, %ch
 	movb  boot_drive, %dl
 	movb  $0x02, %ah
-	movb  $1, %al
+	movb  %bl, %al
 	xorw  %bx, %bx
 do_int13:
 	int   $0x13
 	popw  %si
 	popw  %cx
 	jnc   2f
-	/* A BIOS that refuses a multi-sector EDD read: reset the drive and
-	 * continue one sector per call (the old behaviour). */
-	cmpw  $1, cur_count
-	je    read_error
-	movw  $1, chunk_max
-	movb  boot_drive, %dl
+	movb  boot_drive, %dl     /* reset the drive, then retry */
 	xorw  %ax, %ax
 	int   $0x13
+	incb  tries
+	cmpb  $3, tries
+	jb    load_loop
+	movb  $0, tries
+	cmpw  $1, chunk_max
+	je    read_error
+	movw  $1, chunk_max       /* multi-sector refused: old behaviour */
 	jmp   load_loop
-2:	movw  cur_count, %ax
+2:	movb  $0, tries
+	movw  cur_count, %ax
 	addw  %ax, %si
 	subw  %ax, %cx
 	shlw  $5, %ax
@@ -155,6 +174,8 @@ chunk_max:
 	.word 64
 cur_count:
 	.word 0
+tries:
+	.byte 0
 
 /* INT 13h error: say so instead of halting silently. */
 read_error:
@@ -185,7 +206,7 @@ boot_msg:
 loading_msg:
 	.asciz "Loading kernel...\r\n"
 loaded_msg:
-	.asciz "Kernel loaded, starting...\r\n"
+	.asciz "Kernel loaded\r\n"
 disk_err_msg:
 	.asciz "Disk read error\r\n"
 

+ 21 - 0
cpu.c

@@ -16,6 +16,27 @@ int cpu_has_cpuid(void)
 	return ((a ^ b) & (1u << 21)) != 0;
 }
 
+unsigned int cpu_family_model(void)
+{
+	unsigned int a, b, c, d, family, model;
+
+	if (!cpu_has_cpuid())
+		return 0;
+	a = 0;
+	__asm__ volatile ("cpuid" : "+a"(a), "=b"(b), "=c"(c), "=d"(d));
+	if (a < 1)
+		return 0;
+	a = 1;
+	__asm__ volatile ("cpuid" : "+a"(a), "=b"(b), "=c"(c), "=d"(d));
+	family = (a >> 8) & 0xFu;
+	model = (a >> 4) & 0xFu;
+	if (family == 0xFu)
+		family += (a >> 20) & 0xFFu;
+	if (family == 0x6u || family == 0xFu)
+		model |= ((a >> 16) & 0xFu) << 4;
+	return (family << 8) | model;
+}
+
 void cpu_enable_sse(void)
 {
 	unsigned int eax, ebx, ecx, edx, cr4;

+ 5 - 0
cpu.h

@@ -9,6 +9,11 @@
 /* 1 when CPUID is available (has the ID bit in EFLAGS). */
 int cpu_has_cpuid(void);
 
+/* (display family << 8) | display model from CPUID leaf 1 (extended
+ * family/model folded in the Intel way), or 0 without CPUID.  Used as a
+ * platform allowlist for probes that poke fixed MMIO addresses. */
+unsigned int cpu_family_model(void);
+
 /* 1 when the CPU supports SSE2 (the ION does, the Pentium targets do not). */
 int cpu_has_sse2(void);
 

+ 17 - 9
docs/boot.html

@@ -145,11 +145,15 @@
 		<p>
 			The boot sector carries a FAT16 BPB, loads the contiguous kernel
 			directly after itself to <code>0x7E00</code>, and jumps there.
-			Floppy drives use CHS, one sector per call; fixed disks use
-			<code>INT 13h AH=42h</code> (EDD LBA) with up to 64 sectors per call
-			into <code>ES:0</code>, advancing <code>ES</code> so the offset never
-			wraps (one call per sector used to make the kernel load ~700 BIOS
-			calls). The sector count and the rootfs LBA are patched into the
+			Each <code>INT 13h</code> reads a run of sectors into
+			<code>ES:0</code> and then advances <code>ES</code>, so the offset
+			never wraps: fixed disks use EDD LBA (<code>AH=42h</code>) with up to
+			64 sectors, floppies use CHS up to the end of the track and never
+			across a 64 KB physical ISA DMA boundary. One call per sector used
+			to make the kernel load ~700 BIOS calls, each of which can cost a
+			floppy revolution. A failing run is tried three times, then the
+			loader drops to one sector per call (the old behaviour) before it
+			reports <code>Disk read error</code>. The sector count and the rootfs LBA are patched into the
 			boot sector by the Makefile.
 		</p>
 		<p>
@@ -401,14 +405,18 @@
 			<li>Parse ACPI/MADT; initialise I/O, power, idle, thermal and board sensors.</li>
 			<li>Create the process table, then start the PIT and verify it ticks.</li>
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
-			<li>Initialise PS/2, USB HID and the VGA shadow buffer.</li>
+			<li>Initialise PS/2 (the I2C touchpad probe only on Airmont) and the VGA shadow buffer.</li>
 			<li>Inflate the command pack into <code>/ramfs</code> (table-driven whole-buffer inflate); start the WM and the first frame.</li>
-			<li>Probe the NIC, restore retained network config, start the kworker.</li>
-			<li>Start the <code>bootscan</code> kernel thread (storage probe + FAT mounts, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done) and publish <code>vfs_scan_busy</code>; call <code>wm_server_boot_ready()</code> at once so the WM spawns the first terminal while the disks are probed; run DHCP if needed.</li>
+			<li>Publish <code>vfs_scan_busy</code> and call <code>wm_server_boot_ready()</code>; idle (1 s cap) until the first terminal has presented a frame.</li>
+			<li>Bring up USB (controllers, HID, USB NICs/WiFi/disks) non-preemptibly, as before the WM existed; probe the NIC, restore retained network config, start the kworker.</li>
+			<li>Start the <code>bootscan</code> kernel thread (storage probe + FAT mounts, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done); run DHCP if needed.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 		</ol>
 		<p>
-			The terminal no longer waits for storage: <code>/ramfs</code> and
+			The terminal no longer waits for USB, the NIC or storage.  The
+			WM's network status poll does not trigger the NIC probe (it reports
+			no NIC until <code>kernel_main</code> has probed), so the first frame
+			never waits for a PHY reset.  <code>/ramfs</code> and
 			<code>/share</code> operations (including spawning commands from
 			<code>/ramfs/bin</code>) skip the scan wait, a disk path waits for it,
 			and the scan thread sleeps through its long probe timeouts.  The

+ 1 - 1
docs/development.html

@@ -303,7 +303,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<h2>Debugging</h2>
 		<ul>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
-			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.65 s after the kernel starts.</li>
+			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.4 s after the kernel starts; USB, the NIC and the disks are probed after that (<code>[boot] usb/net/storage</code> lines).</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>

+ 3 - 1
docs/drivers.html

@@ -510,7 +510,9 @@
 			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
+			the two firmware BAR windows for a DesignWare component (only on an
+			Airmont CPU, family 6 model <code>0x4C</code>: elsewhere the scan
+			would reset-cycle 32 fixed MMIO windows for ~64 ms of boot) and
 			<span class="file">i2c.c</span> drives that controller in fast mode,
 			polled and without interrupts.
 		</p>

+ 54 - 34
kernel.c

@@ -24,6 +24,7 @@
 #include "rndis.h"
 #include "rtl8821au.h"
 #include "sched.h"
+#include "sys_wm.h"
 #include "thermal.h"
 #include "timer.h"
 #include "tpec.h"
@@ -501,15 +502,6 @@ void kernel_main(void)
 	boot_val("smp cpus", cpu_count);
 	mouse_init();
 	boot_trace("ps2");
-	/* USB HID backend (mouse + keyboard): controller bring-up + first
-	 * scan; devices plugged in later are picked up by the hotplug
-	 * watcher in the WM loop. */
-	asix_register();
-	usbmsc_register();
-	rtl8821au_register();
-	rndis_register();
-	usbhid_init();
-	boot_trace("usb");
 	vga_shadow_init();
 	vga_fb_flush();
 	boot_trace("vga");
@@ -585,6 +577,51 @@ void kernel_main(void)
 	boot_trace("wm off");
 #endif
 
+	/* Publish the storage scan before anything can run a command: disk
+	 * paths wait for vfs_scan_busy (owned by the idle context until the
+	 * bootscan thread's vfs_mount_all() takes it over), /ramfs and /share
+	 * do not.  Then let the WM spawn the first terminal at once; neither
+	 * it nor the WM needs the NIC probe or the disks below. */
+	vfs_scan_owner_pid = 0;
+	vfs_scan_busy = 1;
+#ifndef NOWM
+	wm_server_boot_ready();
+	boot_trace("terminal ready");
+#else
+	boot_trace("terminal off");
+#endif
+
+#ifndef NOWM
+	/* Let the WM put the first terminal on screen before the USB bring-up
+	 * below: kernel_main is the idle context, so halting here hands the
+	 * CPU to the WM and the new terminal.  Bounded, in case no terminal
+	 * ever presents (missing /ramfs/bin/term). */
+	{
+		unsigned int t0 = get_ticks_ms();
+
+		while (!wm_first_window_presented &&
+		       get_ticks_ms() - t0 < 1000)
+			__asm__ volatile ("sti; hlt");
+	}
+	boot_trace("terminal shown");
+#endif
+
+	/* USB HID backend (mouse + keyboard) and the USB device drivers:
+	 * controller bring-up + first scan; devices plugged in later are
+	 * picked up by the hotplug watcher.  Non-preemptible, exactly like
+	 * the bring-up before the WM existed: port resets and the SOF a
+	 * freshly enabled low-speed device needs within 3 ms must not wait
+	 * for a WM frame.  Before the NIC probe: ASIX/RNDIS/RTL8821AU bind
+	 * during this scan, and USB disks before the bootscan thread. */
+	asix_register();
+	usbmsc_register();
+	rtl8821au_register();
+	rndis_register();
+	preempt_disable();
+	usbhid_init();
+	preempt_enable();
+	boot_trace("usb");
+
 	/* Probe the NIC (link negotiation is asynchronous now) before the
 	 * kworker starts, so its netlog flushes have a device to use. */
 	netdev_active();
@@ -603,35 +640,18 @@ void kernel_main(void)
 	}
 	boot_trace("kworker");
 
-	/* Slow storage bring-up happens behind the usable desktop: publish the
-	 * scan (disk paths wait for it) before the thread even runs, so no
-	 * command can see the half-built boot mount table.  Without a thread
-	 * slot, scan inline as before. */
+	/* Slow storage bring-up happens behind the usable desktop, in a
+	 * thread that may sleep through its probe timeouts.  The scan was
+	 * published above; its vfs_mount_all() takes over the ownership.
+	 * Without a thread slot, scan inline as before. */
 	boot_trace("storage begin");
 	storage_inline = 0;
-	{
-		int pid = process_create("bootscan", boot_storage_main);
-
-		if (pid > 0) {
-			vfs_scan_owner_pid = (unsigned int)pid;
-			vfs_scan_busy = 1;
-		} else {
-			vfs_mount_all();
-			boot_trace("storage");
-			storage_inline = 1;
-		}
+	if (process_create("bootscan", boot_storage_main) <= 0) {
+		vfs_mount_all();
+		boot_trace("storage");
+		storage_inline = 1;
 	}
 
-	/* The WM spawns the first terminal once it sees boot_ready.  The
-	 * terminal and every /ramfs command load without waiting for the
-	 * storage scan; a disk path waits for it (vfs_scan_busy). */
-#ifndef NOWM
-	wm_server_boot_ready();
-	boot_trace("terminal ready");
-#else
-	boot_trace("terminal off");
-#endif
-
 	/* With a linked NIC present, try DHCP; on failure the static default
 	 * stays.  A wired NIC without a cable is skipped so the WiFi fallback
 	 * below does not have to wait out the 4 s timeout first.  Deliberately

+ 7 - 0
sys_net.c

@@ -24,6 +24,7 @@
 #include "process.h"
 #include "tcp.h"
 #include "timer.h"
+#include "wm.h"
 
 /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
 static struct tcp_conn *srv_conn;
@@ -114,6 +115,12 @@ int sys_net_info(struct sys_netinfo *out)
 	 * the PHY/USB with tick-based delays, so run it with interrupts on and
 	 * the BKL released.  Once probed, this is just a cached lookup. */
 	if (!netdev_probed()) {
+		/* Not for the WM: its first top-bar poll comes before its first
+		 * frame, and a NIC probe (PHY reset, USB control transfers) would
+		 * hold that frame back.  kernel_main probes right after the WM
+		 * starts and the WM polls again every second. */
+		if (wm_server_is_wm())
+			return 0;
 		net_enter();
 		nd = netdev_active();
 		net_exit();

+ 3 - 0
sys_wm.c

@@ -169,6 +169,8 @@ int sys_win_create(const struct win_create *in, struct win_info *out)
 /* One serial line when the first application window (normally the boot
  * terminal) presents a frame: the end point of the boot-time measurement
  * ("[boot] ..." lines are relative to the PIT start, this is uptime). */
+volatile int wm_first_window_presented;
+
 static void boot_first_present(void)
 {
 	static int done;
@@ -176,6 +178,7 @@ static void boot_first_present(void)
 	if (done)
 		return;
 	done = 1;
+	wm_first_window_presented = 1;
 	serial_puts(SERIAL_COM1, "wm: first window frame (");
 	serial_puts(SERIAL_COM1, current_proc()->name);
 	serial_puts(SERIAL_COM1, ") at ");

+ 4 - 0
sys_wm.h

@@ -29,4 +29,8 @@ int sys_win_scroll(struct win_scroll *out);
 int sys_wm_resize(unsigned int index, const struct wm_size *in);
 int sys_win_key_wait(unsigned int timeout_ms);
 
+/* Set once the first application window (the boot terminal) has presented
+ * a frame; kernel_main waits for it before the USB bring-up. */
+extern volatile int wm_first_window_presented;
+
 #endif

+ 10 - 0
touchpad.c

@@ -19,6 +19,7 @@
  * touchpad_gesture.c so another touchpad driver can reuse it.
  */
 
+#include "cpu.h"
 #include "touchpad.h"
 #include "touchpad_gesture.h"
 #include "i2c.h"
@@ -338,6 +339,15 @@ int touchpad_init(void)
 
 	if (tp_present)
 		return 1;
+	/* The Elan touchpad sits behind the Braswell LPSS I2C blocks (the HP
+	 * Chromebook, Airmont family 6 model 0x4C).  Everywhere else the scan
+	 * below would only map and reset-cycle 32 fixed MMIO windows that
+	 * belong to nobody (2 ms each, ~64 ms of boot) or, worse, to some
+	 * other device, so skip it. */
+	if (cpu_family_model() != 0x64Cu) {
+		tp_err = "not an Airmont (Braswell) platform";
+		return 0;
+	}
 
 	for (r = 0; r < 2; r++) {
 		for (k = 0; k < TP_CTRL_MAX; k++) {

+ 8 - 3
usb.c

@@ -797,10 +797,15 @@ int usb_scan(usb_probe_fn probe)
 
 	if (scan_busy)
 		return -2;
-	if (usb_hcd_init() != 0)
-		return -1;
-
+	/* Claim the bus before the first controller bring-up: the boot scan
+	 * runs while the WM and the first terminal are already up, and their
+	 * USB calls must see the scan (usb_scan_foreign) rather than half-
+	 * initialized controllers. */
 	scan_begin();
+	if (usb_hcd_init() != 0) {
+		scan_busy = 0;
+		return -1;
+	}
 	generation++;
 	current_probe = probe;
 	print_devices = (probe == 0);

+ 6 - 2
usbhid.c

@@ -332,14 +332,18 @@ static void hid_scan(void)
 
 void usbhid_init(void)
 {
-	if (usb_hcd_init() != 0) {
+	/* usb_scan() brings the controllers up itself, under the scan claim;
+	 * hcd_ok stays 0 until it returns, so usbhid_poll() keeps out. */
+	int r = usb_scan(hid_probe);
+
+	if (r == -1) {
 		serial_puts(SERIAL_COM1, "usbhid: no USB host controller\n");
 		hcd_ok = 0;
 		return;
 	}
 	hcd_ok = 1;
 
-	if (usb_scan(hid_probe) != 0) {
+	if (r != 0) {
 		/* A scan is already running (unlikely at boot): finish from
 		   the watcher. */
 		need_rescan = 1;