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

Add USB Ethernet and finish the X201 e1000e link

Add EHCI plus ASIX AX88772 support, with the USB core, HID and the
existing UHCI/OHCI/xHCI drivers sharing one controller table.  The X201
can now use a USB Ethernet dongle as an independent management link.

Finish the 82577LM e1000e bring-up: keep the warm probe path because the
PCH MAC reset wedges the machine during kexec, classify received frames,
fix the hardware counter offsets and report DHCP rejection details.  The
netdev layer separates the active test NIC from the ASIX management link,
so netmon and net-dev stay reachable while e1000e carries test traffic.

On the X201 this now gets a DHCP lease from the Fritz!Box, resolves DNS,
answers ARP and reaches the internet.  QEMU e1000e and UEFI boot tests
pass.
Johannes Zellner 2 недель назад
Родитель
Сommit
315972b5bb
48 измененных файлов с 6312 добавлено и 356 удалено
  1. 130 66
      AGENTS.md
  2. 61 19
      Makefile
  3. 8 0
      asix.c
  4. 50 2
      bootcon.c
  5. 12 0
      bootcon.h
  6. 38 11
      browser.c
  7. 117 25
      browser_html.c
  8. 3 0
      browser_html.h
  9. 134 14
      dhcp.c
  10. 793 48
      e1000e.c
  11. 1311 0
      ehci.c
  12. 11 0
      ehci.h
  13. 20 0
      kernel.c
  14. 5 3
      kernel.ld
  15. 11 11
      mbr.S
  16. 1 0
      ne2000.c
  17. 55 21
      net.c
  18. 17 0
      net.h
  19. 99 2
      netdev.c
  20. 20 0
      netdev.h
  21. 86 44
      netkexec.c
  22. 15 3
      netlog.c
  23. 81 4
      ngui.c
  24. 4 0
      ngui.h
  25. 1 0
      ohci.c
  26. 1 0
      r8168.c
  27. 2 1
      sys_info.c
  28. 42 0
      sys_net.c
  29. 2 0
      sys_net.h
  30. 4 0
      syscall.c
  31. 2 0
      syscall.h
  32. 52 1
      terminal.c
  33. 7 1
      tools/qmp_cursor_test.py
  34. 11 2
      tools/qmp_keyboard_test.py
  35. 69 0
      ucmd_net100.c
  36. 72 0
      ucmd_netmac.c
  37. 2682 0
      ucmd_solitaire.c
  38. 1 1
      ucmd_term.c
  39. 8 2
      ucmd_wm.c
  40. 1 0
      uhci.c
  41. 216 63
      usb.c
  42. 5 2
      usb.h
  43. 11 4
      usb_hcd.h
  44. 1 1
      usbhid.c
  45. 32 2
      vesa_stage.S
  46. 3 3
      vfs.c
  47. 4 0
      wm_abi.h
  48. 1 0
      xhci.c

+ 130 - 66
AGENTS.md

@@ -14,6 +14,8 @@ make run        # boot floppy.img + root.img in QEMU (fallback: qemu-system-x86_
 make run-smp    # same with -smp 4 (BSP + 3 APs, the Atom 330 topology)
 make run-usb    # boot usb.img as an IDE disk
 make run-uhci   # like `make run` but with a UHCI controller + USB mouse
+make run-ehci   # EHCI + UHCI companion: high-speed storage on EHCI, HID on UHCI
+make run-ehci-asix # pass through a real ASIX AX88772 dongle on EHCI
 make run-net    # like `make run` + a PCI NE2000 NIC on QEMU's user network (slirp; DHCP/DNS/NAT)
 make run-ahci   # like `make run` + an ICH9 AHCI controller and a 512 MB SATA disk (sata0)
 make uefi       # build BOOTX64.EFI and the dual-boot usb-efi.img
@@ -48,13 +50,13 @@ make clean
   targets, an OVMF firmware (`OVMF=/path/to/OVMF.4m.fd` overrides the default).
   `make all` does not build the UEFI image, so a machine without mingw still
   builds the legacy floppy/USB images normally.
-- `usb.img` is 9 MB: partition 1 (type `0x06`, LBA 63, 469 sectors) is the
-  boot sector + kernel and partition 2 (type `0x7F`, LBA 532) is the 8 MB
-  rootfs image (`cmds.img`), immediately behind the kernel.  The header and
-  the 242 KB gzip pack therefore live in the first 1.44 MB, which even a
+- `usb.img` is 9 MB: partition 1 (type `0x06`, LBA 63, 1024 sectors) is the
+  boot sector + kernel and partition 2 (type `0x7F`, LBA 1088) is the 8 MB
+  rootfs image (`cmds.img`), 512 KB behind the boot sector.  The header and
+  the 242 KB gzip pack therefore still live in the first 1.44 MB, which even a
   CHS-only USB BIOS can read.  The boot sector's `pack_lba` field points the
   VESA stage at partition 2.  `usb-efi.img` (17 MB) adds an 8 MB FAT16 ESP
-  as partition 3 (type `0xEF`, LBA 16916); `BOOTX64.EFI` embeds the kernel +
+  as partition 3 (type `0xEF`, LBA 18432); `BOOTX64.EFI` embeds the kernel +
   gz pack.
   `make flash` writes the dual image; `make flash FLASH_IMG=usb.img` writes
   the legacy-only image.  One BPB byte is cleared in the dual image (disk
@@ -70,13 +72,16 @@ make clean
    to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
    reads the `NBROOT1` rootfs header at the boot sector's `pack_lba` field
-   and loads the gzip pack into the low gap at `0x60000..0x9FBFF`
+   and stages the gzip pack in the low gap at `0x60000..0x9FBFF`
    (16 KB staging chunks at `0x3000`, `rep movsb`; no protected-mode BIOS
    call), picks the best VBE mode (≥16 bpp, LFB, fits VRAM, ≤1280px), writes
    mode info to `0x600` and the 12-dword `struct boot_info`
    (magic/checksum/rsdp/flags/pack range) to `0x500`, enables A20, enters
-   protected mode and jumps to `kernel_entry` (`entry.S`, re-points ESP to
-   the current stack top, then `kernel_main`).  The stage prints
+   protected mode, copies the pack to `PACK_HIGH_ADDR` (`0x900000`, above the
+   VGA-shadow reserve) and rewrites the boot-info pack address before jumping
+   to `kernel_entry` (`entry.S`, re-points ESP to the current stack top, then
+   `kernel_main`).  The copy lets the kernel image's BSS/boot stack extend
+   past `0x60000`.  The stage prints
    `Loading rootfs [d=<drive> lba=<hex> e=<0|1> m=<C|L>]...`, then a dot
    after the header and one per 16 KB chunk, so a BIOS hang is localised.
    Drives below 0x80 use CHS (the same path the boot sector's kernel load
@@ -118,11 +123,12 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
 | `0x6C00`  | AP trampoline real-mode stack top (grows down) |
 | `0x6F00`  | AP startup info block (CR3, stack top, C entry, CPU index) |
-| `0x60000` | Legacy rootfs pack (`vesa_stage.S`; compressed gzip data, up to `PACK_LOW_MAX` 0x3FBFF, ending below the EBDA at `0x9FC00`).  Loaded before the kernel; `ramfs_init` copies/inflates it, after which the `0x80000` run area may reuse the space |
-| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs `_end <= 0x60000` for the low rootfs pack and `<= 0x7F000` for the run area).  The low pack leaves only ~6 KB of growth margin |
+| `0x60000` | Legacy rootfs pack staging (`vesa_stage.S`; compressed gzip data, up to `PACK_LOW_MAX` 0x3FBFF, ending below the EBDA at `0x9FC00`).  Temporary: copied to `0x900000` in protected mode before the kernel starts, so the kernel BSS/boot stack may extend past this address |
+| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs `__bss_start <= 0x60000` so the loaded image stays below the legacy staging gap, and `_end <= 0x7F000` so it does not reach the run area).  BSS and the boot stack may extend past `0x60000` |
 | (in-image) | Bootstrap/protected-mode stack: 8 KB `.stack` NOLOAD section, `__boot_stack_top`/`__boot_stack_bottom` (travels with the kernel) |
 | `0x7FF00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
 | `0x80000` | Legacy ring-0 program load area (`run.c`, `serial_recv.c`, `user.ld`; max 0x6000, inside the reserved low 1 MB) |
+| `0x900000`| Legacy boot-time rootfs pack after `vesa_stage.S` relocates it (reserved by `mm_init` until `ramfs_init` takes its copy, then freed; process stacks and kexec staging also start here) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 
 The legacy stage uses `0x3000..0x6FFF` as a 16 KB-chunk rootfs staging
@@ -131,11 +137,12 @@ buffer (`0x4000`) and AP trampoline (`0x6000`) reinitialize those addresses
 later.
 
 The boot-time rootfs pack placement depends on the boot path: the legacy
-stage uses the fixed low gap `0x60000..0x9FBFF`, the UEFI loader puts it at
-the top of RAM below 128 MB, and kexec leaves it in the staging buffer
+stage stages it in the low gap `0x60000..0x9FBFF` and copies it to
+`0x900000` before the kernel starts, the UEFI loader puts it at the top of
+RAM below 128 MB, and kexec leaves it in the staging buffer
 (≥ `0x900000`).  `mm_init` reserves `[pack_addr, pack_addr+pack_size)` from
-boot info when it is above the low 1 MB; the legacy low pack is already
-inside the reserved low 1 MB.  `ramfs_init` copies/inflates it into fresh
+boot info when it is above the low 1 MB, so the legacy pack is protected
+from the frame allocator too.  `ramfs_init` copies/inflates it into fresh
 pages and `mm_free_range()` releases the boot region when it is above the
 VGA shadow reserve.
 
@@ -219,7 +226,8 @@ run area), so adding code no longer moves any address.
   the old kernel's page tables safely.
 - **Window manager**: `wm.c/h` — slot 0 runs `wm_run()`; fixed-size draggable
   terminal windows (64×32) with a system bar (banner + purple "New", "Clock",
-  "Colors" and "Files" buttons + clock + CPU temperature + rainbow). Click-to-focus,
+  "Colors", "Files", "Mines", "Web" and "Cards" buttons + clock + CPU
+  temperature + rainbow). Click-to-focus,
   drag-by-title-bar, close (×). The "New"
   button and first boot spawn terminals; there is no `term` command.  Content is
   repainted per damage rect (`wm_redraw_rect`: desktop fill + every intersecting
@@ -359,10 +367,15 @@ run area), so adding code no longer moves any address.
   no BAR sizing so the ME/AMT cannot leave the BAR undecoded; skips the
   global MAC reset while the link is up, because the PCH/ME wedges on the
   CTRL_RST access), `r8168.c`
-  RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX ring) and `ne2000.c`
-  RTL8029 (QEMU/ISA)). `net.c` — ARP + ICMP echo, IPv4/UDP send + parse helpers,
+  RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX ring), `ne2000.c`
+  RTL8029 (QEMU/ISA) and `asix.c` AX88772 USB Ethernet (bulk over the USB
+  core, so an EHCI/xHCI dongle is usable).  `netdev [name]` is a kernel
+  command that shows the active driver or switches it (`netdev asix`,
+  `netdev e1000e`, ...); it calls the selected driver's probe and leaves the
+  current driver untouched on failure.  `net.c` — ARP + ICMP echo, IPv4/UDP
+  send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
-  decode, `net_checksum()`/`net_checksum_tcp()`. `dhcp.c` — minimal DHCP client
+  decode, `net_checksum()`/`net_checksum_tcp()`.  `dhcp.c` — minimal DHCP client
   (DISCOVER →
   OFFER → REQUEST → ACK; runs at boot when a NIC is present, 3 s budget, keeps
   the static address when no server answers; `dhcp` re-runs it; learns the DNS
@@ -410,15 +423,19 @@ run area), so adding code no longer moves any address.
   keyboard scancodes, watch for plug/unplug.
 - **USB**: `usb.c/h` — controller-agnostic core: device enumeration,
   descriptor parsing, hub recursion, bus scans, per-controller addresses, the
-  hotplug watcher and the interrupt-IN consumers (mouse + keyboard).
-  `usb_hcd.h` is the
-  host-controller ops vtable; `uhci.c` (I/O-port UHCI, frame list at
-  `0x3000`, one instance) and `ohci.c` (MMIO OHCI, up to 4 instances, HCCA +
-  ED/TD lists, polling) implement it.  `lsusb.c` — the `lsusb` command (lists
-  every controller and its tree).  The devices work on both controller types
-  and can be plugged in at any time (root-port connect-change watcher);
-  `lsusb` re-enumerates the bus, after which `usbhid` re-inits itself via a
-  scan generation counter.
+  hotplug watcher and the interrupt-IN consumers (mouse + keyboard).  It keeps
+  a global controller table, so EHCI (high-speed) and its UHCI/OHCI
+  companions (low/full-speed) are driven together; EHCI is scanned first so
+  its port resets hand non-high-speed devices back to the companions.
+  `usb_hcd.h` is the host-controller ops vtable; `uhci.c` (I/O-port UHCI,
+  frame list at `0x3000`, one instance), `ohci.c` (MMIO OHCI, up to 4
+  instances, HCCA + ED/TD lists, polling), `ehci.c` (MMIO EHCI, QH/qTD
+  asynchronous schedule, up to 4 instances) and `xhci.c` (MMIO xHCI, USB2
+  root ports) implement it.  `lsusb.c` — the `lsusb` command (lists every
+  controller and its tree).  The devices work on every controller type and
+  can be plugged in at any time (root-port connect-change watcher); `lsusb`
+  re-enumerates the bus, after which `usbhid` re-inits itself via a scan
+  generation counter.
 - **Power/idle**: `idle.c` — HLT/C1 by default, `idle c2`/`idle c3`
   (and the `c2ap`/`c3ap` AP-only variants) switch to MONITOR/MWAIT at runtime
   (the old USB-legacy SMM hang is addressed by the UHCI/OHCI `0xC0`
@@ -484,8 +501,9 @@ run area), so adding code no longer moves any address.
   `cd` (the shell passes arguments verbatim).  The one argument is passed as a
   C string on the top of the user stack (`process_create_user(..., arg)`), so
   `_start(const char *arg)` needs no crt0.  `ngui.c/h` is the shared userland
-  GUI toolkit linked into `ucmd_files.c` and the external `gallery.elf`
-  (buttons, text fields, icons, layout cuts and scroll panes); `files` opens
+  GUI toolkit linked into `ucmd_files.c`, `ucmd_mines.c`, `ucmd_solitaire.c`
+  and the external `gallery.elf` (buttons, menu items, text fields, icons,
+  layout cuts and scroll panes); `files` opens
   the selected text file through
   `/ramfs/bin/edit`, image files through `/ramfs/bin/view`, and `.htm`/`.html`
   files through `/ramfs/bin/browser`.  `ngui_end_ex()`
@@ -499,6 +517,15 @@ run area), so adding code no longer moves any address.
   `bin/gallery` launcher, which prefers `/share/GALLERY` (development upload
   via `make gallery-dev`), then the pack copy, then the `GALLERY` disk
   fallback on `root.img`/FAT mounts.
+- **Solitaire**: `ucmd_solitaire.c` — a Windows 95 style Klondike game on the
+  canvas service: green felt, a Game/Help menu (ngui buttons and menu items:
+  new game, draw one/three, blue or red deck), four foundations, seven tableau
+  piles, stock/waste, drag-and-drop (or click a card and then its destination),
+  double-click to send a card home, auto-flip, standard scoring, a score/time
+  status bar and the bouncing-card win animation.  The cards use pip layouts
+  with mirrored corner indices, notched corners and pixel-art J/Q/K portraits;
+  the back is the castle.  The bar's "Cards" button launches
+  `/ramfs/bin/solitaire`.
 - **Browser**: the real browser ships in the pack as `bin/browser.app`
   (it links ngui, the full image library, inflate, TLS and uhttp, but the
   rootfs is no longer constrained by the kernel's low-memory window).
@@ -609,7 +636,8 @@ run area), so adding code no longer moves any address.
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [hlt|c2|c3|c2ap|c3ap]`, `spin [ms]`, `help`, `clear`, `serial test|recv`, `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
-DHCP client), `pong` (responds to ARP/ICMP for our IP for 30 s),
+DHCP client), `netdev [name]` (kernel command: show the active NIC driver or
+switch it, e.g. `netdev asix`), `pong` (responds to ARP/ICMP for our IP for 30 s),
 `ping <host|a.b.c.d>` (active ARP + ICMP echo with RTT; resolves DNS names,
 goes via the gateway when the target is off-subnet), `netloop` (PHY loopback
 TX/RX self-test), `netreset`
@@ -638,9 +666,9 @@ filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`, `top`, `spin`,
 `clock`, `colors`, `files`, `gallery`, `view`, `sensors`, `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
-`netreset`, `websrv`, `curl` and `hello` are files in `/ramfs/bin` (packed by the
-Makefile), found through the shell's PATH; the rest are kernel commands
-spawned as processes, or builtins (`cd`, `path`, `help`, `clear`).
+`netreset`, `websrv`, `curl`, `solitaire` and `hello` are files in `/ramfs/bin`
+(packed by the Makefile), found through the shell's PATH; the rest are kernel
+commands spawned as processes, or builtins (`cd`, `path`, `help`, `clear`).
 
 ## Serial hot-reload (kexec) workflow
 
@@ -708,14 +736,18 @@ re-applied on a full floppy/USB boot (C-light reuses the current LFB).
 ## Command pack rootfs (cmds.pack / cmds.img / cmds-dev)
 
 The commands live in `cmds.pack` (`bin/<name>` entries, built by `mkpack`),
-gzip-compressed to `cmds_pack_gz.bin` and wrapped by `mkrootfs` with a
+compressed with `libdeflate-gzip -12` when it is installed (falling back to
+`gzip -9`) to `cmds_pack_gz.bin` and wrapped by `mkrootfs` with a
 one-sector `NBROOT1` header (magic, version, exact `pack_bytes`, checksum,
-gzip flag).  The boot path loads the gz pack into RAM — the legacy stage into
-the low gap at `0x60000`, the UEFI loader via an embedded copy at the top of
+gzip flag).  The build rejects a legacy pack above the `PACK_LOW_MAX` cap;
+`libdeflate` buys several KB over `gzip -9` and is currently needed to fit.
+The boot path loads the gz pack into RAM — the legacy stage stages it in
+the low gap at `0x60000` and relocates it to `0x900000` before the kernel
+starts, the UEFI loader via an embedded copy at the top of
 RAM below 128 MB, kexec via its staging buffer — and `ramfs_init()` inflates
 it into page-allocated memory and mounts it read-only at `/ramfs`.  The
 kernel image no longer contains it, so the pack no longer moves `_end`; the
-legacy low gap has its own size cap (known issue 9).
+legacy staging gap has its own size cap (known issue 9).
 
 `rootfs.bin` is the floppy append (header + pack); `cmds.img` is the 8 MB
 rootfs partition image written to USB partition 2.  `cmds-dev`/`net-cmds`
@@ -797,11 +829,36 @@ pauses while a net command owns the receiver.
 ## USB HID (mouse + keyboard) / host controllers
 
 `usb.c` is controller-agnostic; the drivers implement the `usb_hcd_ops`
-vtable (`usb_hcd.h`): control transfers, root-port queries/reset, and two
-interrupt-IN slots (0 = mouse, 1 = keyboard). `usb_hcd_init()` picks UHCI
-when present, otherwise OHCI (`ohci.c` drives up to 4 instances — MCP79 has
-four).
-
+vtable (`usb_hcd.h`): control transfers, root-port queries/reset, bulk
+transfers and up to six interrupt-IN slots. `usb_hcd_init()` brings up every
+controller type and builds one global controller table: EHCI first, then
+UHCI, OHCI and xHCI.  A machine with EHCI and UHCI/OHCI companions therefore
+enumerates high-speed devices on EHCI and low/full-speed devices on the
+companions; a port reset hands a non-high-speed device back to its companion
+before the companion is scanned.  A single device address is per-controller,
+so addresses do not collide.
+
+- **EHCI**: MMIO BAR0, page-allocated frame list/QH/qTD/bounce buffers, QH/
+  qTD descriptors in the identity-mapped kernel space.  Control, bulk and
+  interrupt endpoints all live in the asynchronous schedule (a circular QH
+  list with the control QH after the reclamation head); the periodic list is
+  not used.  Polling only (no IRQs).  Ports are claimed from the companions
+  with CONFIGFLAG; `port_reset` holds PORT_RESET for 50 ms, then clears it
+  (QEMU and hardware complete the reset on that write), waits for PED and
+  sets PORT_OWNER for a non-high-speed device.  `ehci.c` supports up to four
+  PCI instances.  Bulk `in`/`out` use one qTD per call (up to five 4 KB
+  buffer pages), which is enough for the ASIX AX88772 RX/TX path.
+  There is no transaction translator: low/full-speed devices behind a
+  high-speed hub stay invisible to EHCI (use the companion controller for
+  them).
+- **Hubs**: `usb.c` sets a class-9 hub's configuration with
+  `SET_CONFIGURATION` before issuing hub-class requests, powers every
+  downstream port (honouring the hub descriptor's `bPwrOn2PwrGood`) and waits
+  for `PORT_ENABLE` after the port reset.  This is needed for the X201's PCH
+  internal `8087:0020` hubs, which sit between the EHCI root ports and the
+  external USB ports.  Downstream devices are enumerated recursively; a
+  full-speed device behind a USB 2.0 hub would need split transactions (not
+  implemented).
 - **OHCI**: BAR0 MMIO (`paging_map_mmio()`), HCCA from the page allocator,
   static 16-byte-aligned ED/TD sets. Control transfers run on the control
   list (`HcControlHeadED` + CLF) and are polled via each TD's condition code
@@ -842,19 +899,21 @@ four).
   can't leave the devices dead.  The BIOS PS/2 legacy emulation is
   deliberately disabled (PCI config `0xC0`) because the kernel now owns
   these devices.
-- **Debug**: `ohci:` / `uhci:` / `usbhid:` lines go to COM1 (and through the
-  netlog mirror on serial-less boards); `mouseinfo` shows the active mouse
-  backend and whether a USB keyboard is attached; `usb.c`'s `USB_DEBUG` and
-  `usbhid.c`'s `USBHID_DEBUG` (both default 0) add scan/report tracing.
-  Headless tests: `make test-usb` runs `tools/qmp_cursor_test.py` (injects
-  mouse motion with QMP `input-send-event`, verifies the cursor with
-  `screendump`) and `tools/qmp_keyboard_test.py` (types with QMP `send-key`,
-  verifies the screen output); both take mode flags (`--uhci`, `--uhci72`,
-  `--multi`, `--hotplug`).
-- **Limits**: no EHCI (high-speed devices stay invisible), hotplug watches
-  root ports only (a device behind a hub is only enumerated when a scan
-  runs), HID report IDs are not handled (a device that prepends report IDs
-  decodes as garbage), absolute pointers/tablets are ignored, no wheel
+- **Debug**: `ohci:` / `uhci:` / `ehci:` / `usbhid:` lines go to COM1 (and
+  through the netlog mirror on serial-less boards); `mouseinfo` shows the
+  active mouse backend and whether a USB keyboard is attached; `usb.c`'s
+  `USB_DEBUG` and `usbhid.c`'s `USBHID_DEBUG` (both default 0) add
+  scan/report tracing.  Headless tests: `make test-usb` runs
+  `tools/qmp_cursor_test.py` (injects mouse motion with QMP
+  `input-send-event`, verifies the cursor with `screendump`) and
+  `tools/qmp_keyboard_test.py` (types with QMP `send-key`, verifies the
+  screen output); the cursor test takes `--uhci`, `--uhci72` and `--multi`,
+  and both take `--hotplug`, `--xhci` and `--ehci`.
+- **Limits**: EHCI drives high-speed root-port devices only (no TT/split
+  transactions for low/full-speed devices behind a high-speed hub), hotplug
+  watches root ports only (a device behind a hub is only enumerated when a
+  scan runs), HID report IDs are not handled (a device that prepends report
+  IDs decodes as garbage), absolute pointers/tablets are ignored, no wheel
   events, and no USB keyboard means no keyboard at all if the BIOS legacy
   emulation was the only other source (`make net-dev` still reloads the
   kernel).
@@ -1024,10 +1083,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 9. The rootfs pack is inflated into one contiguous page-allocated run at
    boot, capped by `RAMFS_MAX_DECOMPRESSED` (8 MB); a larger pack fails to
    load (the USB rootfs partition is 8 MB, so this only bites if the cap is
-   raised without a bigger partition).  The legacy low pack is additionally
-   capped at `PACK_LOW_MAX` (0x3FBFF, ~255 KB compressed) by the
-   `0x60000..0x9FBFF` gap; a larger pack needs the UEFI/kexec path or a
-   high-memory legacy copy.
+   raised without a bigger partition).  The legacy stage still stages the
+   compressed pack through the `0x60000..0x9FBFF` gap before copying it to
+   `0x900000`, so `PACK_LOW_MAX` (0x3FBFF, ~255 KB compressed) still caps the
+   legacy path; a larger pack needs the UEFI/kexec path or a chunked
+   high-memory loader.  The current pack is within a few KB of that cap
+   with `libdeflate-gzip -12` (plain `gzip -9` is already over), so command
+   growth needs care.
 10. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
     board that exposes only MP tables stays UP (deliberate; there is no test
     hardware).
@@ -1122,10 +1184,12 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     is unsigned, so Secure Boot must be off.  The loader needs a GOP mode with
     BGRX or RGBX 32bpp, width ≤ 1280, `pitch * height` ≤ 8 MB and the
     framebuffer below 4 GB; otherwise the kernel starts headless (serial only).
-    There is no xHCI/EHCI driver, so many modern UEFI laptops/desktops boot the
-    UI without a USB keyboard or mouse (PS/2 may still work), and there is no
-    IOAPIC setup: OVMF delivers the ISA IRQs through the 8259 path, but a
-    platform that routes them through the IOAPIC may need a mask-all step after
-    ACPI init.  UEFI runtime services are not used, so `reboot`/`poweroff` still
-    use the legacy ports.  The image is 17 MB: the 9 MB kernel + rootfs disk
-    and the 8 MB ESP.
+    xHCI drives USB2 root ports only (USB3 SuperSpeed devices are not
+    handled), and EHCI only high-speed root ports (no transaction translator
+    for low/full-speed devices behind a high-speed hub), so a USB3-only
+    keyboard/mouse can still leave the UI without input (PS/2 may still work).
+    There is no IOAPIC setup: OVMF delivers the ISA IRQs through the 8259
+    path, but a platform that routes them through the IOAPIC may need a
+    mask-all step after ACPI init.  UEFI runtime services are not used, so
+    `reboot`/`poweroff` still use the legacy ports.  The image is 17 MB: the
+    9 MB kernel + rootfs disk and the 8 MB ESP.

+ 61 - 19
Makefile

@@ -31,7 +31,7 @@ EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
 
-.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan kernel-dev kernel-net-dev rootfs
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan kernel-dev kernel-net-dev rootfs
 
 all: floppy.img usb.img final.bin root.img
 
@@ -161,6 +161,9 @@ uhci.o: uhci.c uhci.h usb_hcd.h pci.h serial.h timer.h
 ohci.o: ohci.c ohci.h usb_hcd.h pci.h paging.h mm.h serial.h timer.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
+ehci.o: ehci.c ehci.h usb_hcd.h pci.h paging.h mm.h serial.h timer.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
+
 xhci.o: xhci.c xhci.h usb_hcd.h pci.h paging.h mm.h serial.h timer.h spinlock.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
@@ -195,7 +198,7 @@ r8168.o: r8168.c r8168.h netdev.h io.h mm.h paging.h pci.h process.h serial.h ti
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
 e1000e.o: e1000e.c e1000e.h io.h mm.h netdev.h paging.h pci.h process.h serial.h timer.h
-	$(CC) $(CFLAGS) -Os -o $@ $<
+	$(CC) $(CFLAGS) -Oz -o $@ $<
 
 netkexec.o: netkexec.c netkexec.h io.h kexec.h net.h netdev.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -332,7 +335,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
 	$(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 run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.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 prog_embed.o space_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 run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_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 prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -370,12 +373,12 @@ BPB_HEADS_OFFSET = 26
 BPB_HIDDEN_OFFSET = 28
 BPB_TOTAL_OFFSET = 32
 
-# USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF).  The rootfs
-# sits immediately behind the kernel (LBA 532), so even a CHS-only USB BIOS
-# finds the header and the 242 KB gzip pack inside the first 1.44 MB.
-ROOTFS_LBA     = 532
+# USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF).  Partition 1
+# has room for a 512 KB kernel; the rootfs starts after it at LBA 1088.
+# The header and the 242 KB gzip pack are still inside the first 1.44 MB.
+ROOTFS_LBA     = 1088
 ROOTFS_SECTORS = 16384
-ESP_LBA        = 16916
+ESP_LBA        = 18432
 
 # Patch a little-endian 16/32-bit value into a boot-sector copy.  Usage:
 #   $(call patch16,file,offset,value)
@@ -409,13 +412,13 @@ usb.img: mbr.bin boot.bin kernel.bin cmds.img
 	$(call patch32,$@.boot.tmp,$(ROOTFS_LBA),$(PACK_LBA_OFFSET)); \
 	printf '\xff\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HEADS_OFFSET) conv=notrunc 2>/dev/null; \
 	printf '\x3f\x00\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HIDDEN_OFFSET) conv=notrunc 2>/dev/null; \
-	printf '\xd5\x01\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
+	printf '\x00\x04\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
 	cat mbr.bin > $@; \
 	dd if=/dev/zero bs=512 count=62 2>/dev/null >> $@; \
 	cat $@.boot.tmp kernel.bin >> $@; \
 	truncate -s 9M $@; \
 	dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
-	printf '\x00\x08\x1d\x00\x7f\x0d\x20\x01\x14\x02\x00\x00\x00\x40\x00\x00' | \
+	printf '\x00\x11\x12\x00\x7f\x16\x15\x01\x40\x04\x00\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
 	rm -f $@.boot.tmp
 
@@ -481,9 +484,9 @@ usb-efi.img: usb.img esp.img
 	# the kernel at LBA 64.  Clear BPB FATSize16 so the firmware rejects the
 	# fake volume; boot.S and the BIOS never read that field.
 	printf '\x00\x00' | dd of=$@ bs=1 seek=$$((63 * 512 + 0x16)) conv=notrunc 2>/dev/null
-	# Partition 3: ESP at LBA 16916, type 0xEF, 8 MB (partition 2 is the
-	# rootfs at LBA 532 written by usb.img).
-	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x14\x42\x00\x00\x00\x40\x00\x00' | \
+	# Partition 3: ESP at LBA 18432, type 0xEF, 8 MB (partition 2 is the
+	# rootfs at LBA 1088 written by usb.img).
+	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x00\x48\x00\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=478 conv=notrunc 2>/dev/null
 	dd if=esp.img of=$@ bs=512 seek=$(ESP_LBA) conv=notrunc 2>/dev/null
 	truncate -s $$(( ($(ESP_LBA) + $(ESP_SECTORS)) * 512 )) $@
@@ -536,9 +539,9 @@ UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
         ucmd_clock ucmd_colors ucmd_edit ucmd_wm ucmd_term ucmd_spin \
         ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
         ucmd_netsend ucmd_netrecv ucmd_ping ucmd_pong ucmd_netlog \
-        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_curl \
-        ucmd_files ucmd_view ucmd_sensors ucmd_mines ucmd_gallery \
-        ucmd_websrv ucmd_browser
+        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_net100 ucmd_netmac ucmd_curl \
+        ucmd_files ucmd_view ucmd_sensors ucmd_mines ucmd_solitaire \
+        ucmd_gallery ucmd_websrv ucmd_browser
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -596,6 +599,14 @@ ucmd_mines.elf: ucmd_mines.o ngui.o font_8x16.o ucmd.ld
 	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
 	$(OBJCOPY) --strip-all $@ $@
 
+# solitaire: a Windows 95 style Klondike game on the same canvas toolkit.
+ucmd_solitaire.o: ucmd_solitaire.c ucmd_lib.h syscall.h wm_abi.h ngui.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
+
+ucmd_solitaire.elf: ucmd_solitaire.o ngui.o font_8x16.o ucmd.ld
+	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
+	$(OBJCOPY) --strip-all $@ $@
+
 # gallery: a live showcase of every ngui widget and primitive.  The real ELF
 # ships in the rootfs pack as bin/gallery.app; ucmd_gallery.c (in UCMDS) is
 # the tiny PATH launcher (which also accepts /share/GALLERY).
@@ -707,9 +718,19 @@ cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf browser.elf gallery.elf mkpack
 	./mkpack $@ $(PACK_FILES)
 
 # The boot-time rootfs is gzip-compressed; the raw cmds.pack is still what
-# cmds-dev/net-cmds send for a runtime replacement.
+# cmds-dev/net-cmds send for a runtime replacement.  Prefer libdeflate's
+# level 12 when it is installed: it saves about 8 KB over gzip -9 on the
+# current pack, which matters for the legacy low-memory pack (0x3FBFF).
+CMDS_GZ     = $(shell command -v libdeflate-gzip >/dev/null 2>&1 && \
+		     echo "libdeflate-gzip -12 -n -c" || echo "gzip -9 -n -c")
+CMDS_GZ_MAX = 261119
 cmds_pack_gz.bin: cmds.pack
-	gzip -9 -n -c $< > $@
+	$(CMDS_GZ) $< > $@
+	@sz=$$(stat -c%s $@); \
+	if [ $$sz -gt $(CMDS_GZ_MAX) ]; then \
+		echo "error: cmds_pack_gz.bin is $$sz bytes; the legacy low pack cap is $(CMDS_GZ_MAX) (install libdeflate-gzip)"; \
+		rm -f $@; exit 1; \
+	fi
 
 mkrootfs: mkrootfs.c rootfs.h
 	gcc -O2 -o $@ $<
@@ -823,11 +844,30 @@ run-xhci: floppy.img root.img
 	qemu-system-i386 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
+# EHCI variant (USB 2.0 high-speed root ports).  usb-storage is high-speed
+# and lands on EHCI; the mouse/keyboard are full-speed and use the UHCI
+# companion, exercising the multi-controller USB core.
+run-ehci: floppy.img root.img
+	qemu-system-i386 -M pc -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
+	  -device usb-storage,bus=ehci0.0,drive=usbstick \
+	  -drive if=none,id=usbstick,file=root.img,format=raw \
+	  -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
+	  -drive file=floppy.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
+	  -device usb-storage,bus=ehci0.0,drive=usbstick \
+	  -drive if=none,id=usbstick,file=root.img,format=raw \
+	  -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
+	  -drive file=floppy.img,format=raw,if=floppy
+
 # Passthrough of the real ASIX AX88772 dongle (requires the host's asix
 # driver to be unbound and permission on /dev/bus/usb/...; typically run with
 # sudo).  `-nic none` keeps the QEMU e1000 out of the way so the USB NIC is
 # the only netdev.
 ASIX_HOST ?= vendorid=0x0b95,productid=0x7720
+run-ehci-asix: floppy.img root.img
+	qemu-system-i386 -M pc -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
+	qemu-system-x86_64 -M pc -nic none -device usb-ehci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+
 run-xhci-asix: floppy.img root.img
 	qemu-system-i386 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
@@ -880,9 +920,11 @@ test-usb: floppy.img root.img
 	python3 tools/qmp_cursor_test.py --uhci72
 	python3 tools/qmp_cursor_test.py --hotplug
 	python3 tools/qmp_cursor_test.py --xhci
+	python3 tools/qmp_cursor_test.py --ehci
 	python3 tools/qmp_keyboard_test.py
 	python3 tools/qmp_keyboard_test.py --hotplug
 	python3 tools/qmp_keyboard_test.py --xhci
+	python3 tools/qmp_keyboard_test.py --ehci
 
 scp-final: final.bin
 	scp final.bin root@192.168.0.136:
@@ -988,4 +1030,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 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 run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.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 prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o 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 bootstrap.o bootstrap.elf bootstrap.bin send_serial 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
+	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 run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_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 prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o 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 bootstrap.o bootstrap.elf bootstrap.bin send_serial 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

+ 8 - 0
asix.c

@@ -348,6 +348,9 @@ static void net_reset(void)
 		chip_init();
 }
 
+static const unsigned char asix_default_ip[4] = { 192, 168, 50, 2 };
+static const unsigned char asix_default_mask[4] = { 255, 255, 255, 0 };
+
 const struct netdev asix_netdev = {
 	"asix",
 	"AX88772 USB 2.0 Ethernet",
@@ -359,6 +362,11 @@ const struct netdev asix_netdev = {
 	0,
 	0,
 	net_reset,
+	0,
+	0,
+	asix_default_ip,
+	asix_default_mask,
+	0,
 };
 
 /* Read and parse the configuration descriptor.  The AX88772 has one

+ 50 - 2
bootcon.c

@@ -8,6 +8,7 @@
 #include "bootcon.h"
 #include "bootinfo.h"
 #include "font_8x8.h"
+#include "mm.h"
 #include "serial.h"
 #include "vga.h"
 
@@ -25,6 +26,16 @@ static int enabled;
 static unsigned int cur_x, cur_y;
 static unsigned int home_x, home_y;
 
+/* Text log kept independently of the framebuffer: bootcon is disabled when
+ * the WM starts, but the e1000e probe and later driver messages still go
+ * through bootcon_puts().  The buffer is allocated from the frame allocator
+ * (see bootcon_log_init) so it does not move the kernel's _end past the low
+ * rootfs pack at 0x60000. */
+static char *log_buf;
+static unsigned int log_cap;
+static unsigned int log_len;
+static int log_truncated;
+
 static void fb_px(unsigned int x, unsigned int y, unsigned int r,
 		  unsigned int g, unsigned int b)
 {
@@ -151,14 +162,40 @@ static void newline(void)
 	clear_line();
 }
 
-void bootcon_puts(const char *s)
+void bootcon_log_init(void)
 {
-	if (!fb_ok || !enabled)
+	unsigned int mem;
+
+	if (log_buf)
 		return;
+	mem = mm_alloc_pages(BOOTCON_LOG_PAGES);
+	if (!mem)
+		return;
+	log_buf = (char *)mem;
+	log_cap = BOOTCON_LOG_PAGES * PAGE_SIZE;
+	log_len = 0;
+	log_truncated = 0;
+	log_buf[0] = '\0';
+}
 
+void bootcon_puts(const char *s)
+{
 	while (*s) {
 		unsigned char c = (unsigned char)*s++;
 
+		/* Keep the text even after bootcon_set_enabled(0) has hidden
+		 * the framebuffer console. */
+		if (log_buf) {
+			if (log_len + 1 < log_cap) {
+				log_buf[log_len++] = (char)c;
+				log_buf[log_len] = '\0';
+			} else {
+				log_truncated = 1;
+			}
+		}
+
+		if (!fb_ok || !enabled)
+			continue;
 		if (c == '\r') {
 			cur_x = LEFT;
 			continue;
@@ -174,6 +211,17 @@ void bootcon_puts(const char *s)
 	}
 }
 
+const char *bootcon_log(unsigned int *len)
+{
+	*len = log_len;
+	return log_buf ? log_buf : "";
+}
+
+int bootcon_log_truncated(void)
+{
+	return log_truncated;
+}
+
 void bootcon_dec(unsigned int v)
 {
 	char buf[12];

+ 12 - 0
bootcon.h

@@ -10,6 +10,8 @@
 #ifndef BOOTCON_H
 #define BOOTCON_H
 
+#define BOOTCON_LOG_PAGES 8
+
 void bootcon_init(void);
 int  bootcon_ok(void);
 void bootcon_set_enabled(int on);
@@ -19,4 +21,14 @@ void bootcon_hex(unsigned int v);
 /* One line with the cached mode geometry and LFB address. */
 void bootcon_fb_info(void);
 
+/* Allocate the RAM log after the page allocator is up.  The buffer is taken
+ * from the frame allocator, not BSS: the low rootfs pack limits _end to
+ * 0x60000 and a 16 KB static log would overlap it. */
+void bootcon_log_init(void);
+/* Everything written through bootcon_puts() after bootcon_log_init(),
+ * including text produced after the WM has taken over the framebuffer.  The
+ * returned buffer is NUL terminated; *len is the number of valid bytes. */
+const char *bootcon_log(unsigned int *len);
+int bootcon_log_truncated(void);
+
 #endif

+ 38 - 11
browser.c

@@ -134,6 +134,9 @@ static int cancel_fetch;
 static int quit;
 
 static struct ngui_rect page_rect;
+static int layout_valid, layout_left, layout_right;
+static unsigned int layout_nnode;
+static int layout_content_h;
 
 /* layout output */
 struct lay_run {
@@ -1175,6 +1178,7 @@ static void show_error(const char *title, const char *detail)
 	s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
 		 sizeof(error_page));
 	br_parse(&doc, page, n);
+	layout_valid = 0;
 	preload_active = 0;
 	preload_pending = 0;
 	preload_wait = 0;
@@ -1242,6 +1246,7 @@ static int load_stylesheets(void)
 	}
 	br_parse_inline_css(&doc);
 	br_reset_styles(&doc);
+	layout_valid = 0;
 	return 0;
 }
 
@@ -1328,6 +1333,7 @@ static void preload_step(void)
 			nd->img_index = (short)image_count;
 			image_count++;
 			preload_loaded++;
+			layout_valid = 0;
 		}
 		ngui_invalidate(&ui);
 		return;
@@ -1380,6 +1386,7 @@ static void load_page(const char *url, int local, int push)
 
 	heap_clear();
 	image_reset();
+	layout_valid = 0;
 	page_scroll.offset = 0;
 	page_scroll.dragging = 0;
 	br_parse_plain(&doc, "", 0);    /* blank while loading */
@@ -1480,6 +1487,7 @@ static void load_home(int push)
 	preload_pending = 0;
 	heap_clear();
 	image_reset();
+	layout_valid = 0;
 	page_scroll.offset = 0;
 	page_scroll.dragging = 0;
 	br_parse_plain(&doc, "", 0);
@@ -1628,7 +1636,7 @@ static int run_width(const struct lay_run *r)
 
 static void flush_line(struct lay *l)
 {
-	int total, x, i;
+	int total, x, i, vis;
 
 	if (!l->nruns && l->line_x == l->left) {
 		l->line_h = 0;
@@ -1636,6 +1644,13 @@ static void flush_line(struct lay *l)
 	}
 	total = l->line_x - l->left;
 	x = l->left;
+	vis = 1;
+	if (l->draw) {
+		int lh = l->line_h ? l->line_h : 16;
+
+		vis = l->line_y + lh >= page_rect.y &&
+		      l->line_y <= page_rect.y + page_rect.h;
+	}
 	if (l->align == BR_ALIGN_CENTER)
 		x += (l->right - l->left - total) / 2;
 	else if (l->align == BR_ALIGN_RIGHT)
@@ -1647,7 +1662,7 @@ static void flush_line(struct lay *l)
 		int w = run_width(r);
 
 		x += r->gap;
-		if (l->draw) {
+		if (l->draw && vis) {
 			if (r->img == -2) {
 				int hh = r->ih ? r->ih : 20;
 
@@ -2212,14 +2227,18 @@ static void layout_block(struct lay *l, unsigned int node,
 	r0 = old_right - st->mr;
 	if (r0 < l0 + 16)
 		r0 = l0 + 16;
-	if (l->draw && st->has_bg && block_h[node])
-		ngui_fill_rect(l->ui, (struct ngui_rect){l0, top, r0 - l0,
-							 block_h[node]},
-			       st->bg);
-	if (l->draw && st->border_w && block_h[node])
-		ngui_frame(l->ui, (struct ngui_rect){l0, top, r0 - l0,
-						    block_h[node]},
-			   st->border_color);
+	if (l->draw && block_h[node] &&
+	    top < page_rect.y + page_rect.h &&
+	    top + block_h[node] > page_rect.y) {
+		if (st->has_bg)
+			ngui_fill_rect(l->ui, (struct ngui_rect){l0, top,
+						 r0 - l0, block_h[node]},
+				       st->bg);
+		if (st->border_w)
+			ngui_frame(l->ui, (struct ngui_rect){l0, top,
+						 r0 - l0, block_h[node]},
+				   st->border_color);
+	}
 	l->left = l0 + st->pl;
 	l->right = r0 - st->pr;
 	if (l->right < l->left + 8)
@@ -3051,7 +3070,15 @@ static void draw_page(struct ngui_rect page)
 		return;
 	left = page.x + 8;
 	right = page.x + page.w - 16;
-	ch = measure_page(left, right);
+	if (!layout_valid || left != layout_left || right != layout_right ||
+	    layout_nnode != doc.nnode) {
+		layout_content_h = measure_page(left, right);
+		layout_valid = 1;
+		layout_left = left;
+		layout_right = right;
+		layout_nnode = doc.nnode;
+	}
+	ch = layout_content_h;
 	page_scroll.content_h = ch;
 	page_scroll.view_h = page.h;
 	ngui_scroll_begin(&ui, page, &page_scroll);

+ 117 - 25
browser_html.c

@@ -79,10 +79,46 @@ const char *br_tag_name(unsigned int tag)
 	return "?";
 }
 
+static unsigned int s_len(const char *s);
+
+#define TAG_HASH_SIZE 128
+
+static unsigned char tag_hash_idx[TAG_HASH_SIZE];
+static int tag_hash_ready;
+
+static unsigned int tag_hash_key(const char *name, unsigned int len)
+{
+	unsigned int i, h = 2166136261u;
+
+	for (i = 0; i < len; i++) {
+		h ^= (unsigned char)name[i];
+		h *= 16777619u;
+	}
+	return h & (TAG_HASH_SIZE - 1);
+}
+
+static void tag_hash_build(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < TAG_HASH_SIZE; i++)
+		tag_hash_idx[i] = 0xFF;
+	for (i = 0; tag_names[i].name; i++) {
+		unsigned int h = tag_hash_key(tag_names[i].name,
+					      s_len(tag_names[i].name));
+
+		while (tag_hash_idx[h] != 0xFF)
+			h = (h + 1) & (TAG_HASH_SIZE - 1);
+		tag_hash_idx[h] = (unsigned char)i;
+	}
+	tag_hash_ready = 1;
+}
+
 unsigned int br_tag_from_name(const char *name)
 {
-	unsigned int i, j;
 	char lower[16];
+	unsigned int i, len, h;
+	unsigned char idx;
 
 	for (i = 0; i < 15 && name[i]; i++) {
 		char c = name[i];
@@ -92,13 +128,25 @@ unsigned int br_tag_from_name(const char *name)
 		lower[i] = c;
 	}
 	lower[i] = '\0';
-	for (i = 0; tag_names[i].name; i++) {
-		for (j = 0; lower[j] && tag_names[i].name[j] == lower[j]; j++)
-			;
-		if (!lower[j] && !tag_names[i].name[j])
-			return tag_names[i].tag;
+	len = i;
+	if (!tag_hash_ready)
+		tag_hash_build();
+	h = tag_hash_key(lower, len);
+	for (;;) {
+		idx = tag_hash_idx[h];
+		if (idx == 0xFF)
+			return BR_TAG_UNKNOWN;
+		{
+			const char *n = tag_names[idx].name;
+			unsigned int j;
+
+			for (j = 0; lower[j] && n[j] == lower[j]; j++)
+				;
+			if (!lower[j] && !n[j])
+				return tag_names[idx].tag;
+		}
+		h = (h + 1) & (TAG_HASH_SIZE - 1);
 	}
-	return BR_TAG_UNKNOWN;
 }
 
 int br_tag_is_void(unsigned int tag)
@@ -418,23 +466,41 @@ static unsigned int store_raw(struct br_doc *d, const char *s,
 	return off;
 }
 
+#define VAR_HASH_SIZE 1024
+
+static unsigned int var_hash(const char *name, unsigned int len)
+{
+	unsigned int i, h = 2166136261u;
+
+	for (i = 0; i < len; i++) {
+		h ^= (unsigned char)name[i];
+		h *= 16777619u;
+	}
+	return h & (VAR_HASH_SIZE - 1);
+}
+
 static unsigned int css_var_find(struct br_doc *d, const char *name,
 				 unsigned int nlen)
 {
-	unsigned int i;
+	unsigned int h = var_hash(name, nlen);
 
-	for (i = 0; i < d->nvars; i++) {
-		const char *n = attr_ptr(d, d->var_name[i]);
-		unsigned int j;
+	for (;;) {
+		unsigned int i = d->var_slot[h];
 
-		if (s_len(n) != nlen)
-			continue;
-		for (j = 0; j < nlen && n[j] == name[j]; j++)
-			;
-		if (j == nlen)
-			return i;
+		if (!i)
+			return BR_MAX_VARS;
+		i--;
+		if (s_len(attr_ptr(d, d->var_name[i])) == nlen) {
+			const char *n = attr_ptr(d, d->var_name[i]);
+			unsigned int j;
+
+			for (j = 0; j < nlen && n[j] == name[j]; j++)
+				;
+			if (j == nlen)
+				return i;
+		}
+		h = (h + 1) & (VAR_HASH_SIZE - 1);
 	}
-	return BR_MAX_VARS;
 }
 
 static void css_var_set(struct br_doc *d, const char *name,
@@ -450,10 +516,16 @@ static void css_var_set(struct br_doc *d, const char *name,
 		return;
 	i = css_var_find(d, name, nlen);
 	if (i >= BR_MAX_VARS) {
+		unsigned int h;
+
 		if (d->nvars >= BR_MAX_VARS)
 			return;
 		i = d->nvars++;
 		d->var_name[i] = noff;
+		h = var_hash(name, nlen);
+		while (d->var_slot[h])
+			h = (h + 1) & (VAR_HASH_SIZE - 1);
+		d->var_slot[h] = (unsigned short)(i + 1);
 	}
 	d->var_val[i] = voff;
 }
@@ -641,6 +713,8 @@ void br_doc_init(struct br_doc *d)
 	d->attrs[0] = '\0';
 	d->nvars = 0;
 	d->root = 0;
+	for (unsigned int vi = 0; vi < VAR_HASH_SIZE; vi++)
+		d->var_slot[vi] = 0;
 	root_font_px = 16;
 }
 
@@ -2277,6 +2351,8 @@ static void rule_index_add(struct br_doc *d, unsigned int idx,
 	*head = (unsigned short)idx;
 }
 
+static unsigned int selector_specificity(const char *s, unsigned int len);
+
 static int css_collect_only;
 
 static void parse_css(struct br_doc *d, const char *s, unsigned int len)
@@ -2409,6 +2485,17 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 					r->sel_off = off;
 					r->key_off = store_attr(d, sel + ks,
 								 k - ks, 79);
+					r->spec = (unsigned short)selector_specificity(sel, k);
+					r->single = 1;
+					{
+						unsigned int z;
+
+						for (z = 0; z < k; z++)
+							if (is_space(sel[z]) || sel[z] == '>') {
+								r->single = 0;
+								break;
+							}
+					}
 					r->nd = (unsigned char)nd;
 					for (k = 0; k < nd; k++)
 						r->d[k] = decls[k];
@@ -2665,7 +2752,7 @@ static unsigned int selector_specificity(const char *s, unsigned int len)
 
 /* Match one complex selector; returns specificity or -1. */
 static int selector_match(struct br_doc *d, unsigned int node, const char *s,
-			  unsigned int len)
+			  unsigned int len, int right_done)
 {
 	struct {
 		unsigned int start, end;
@@ -2702,7 +2789,8 @@ static int selector_match(struct br_doc *d, unsigned int node, const char *s,
 	}
 	if (!np)
 		return -1;
-	if (!compound_match(d, node, s + parts[np - 1].start,
+	if (!right_done &&
+	    !compound_match(d, node, s + parts[np - 1].start,
 			    parts[np - 1].end - parts[np - 1].start))
 		return -1;
 	right = np - 1;
@@ -2746,12 +2834,16 @@ static void rule_try(struct br_doc *d, unsigned int node, unsigned int idx)
 {
 	struct br_rule *r = &d->rules[idx];
 	const char *key = attr_ptr(d, r->key_off);
-	int spec;
+	int spec, key_ok = 0;
 
-	if (key[0] && !compound_match(d, node, key, s_len(key)))
-		return;
-	spec = selector_match(d, node, attr_ptr(d, r->sel_off),
-			      s_len(attr_ptr(d, r->sel_off)));
+	if (key[0]) {
+		if (!compound_match(d, node, key, s_len(key)))
+			return;
+		key_ok = 1;
+	}
+	spec = (r->single && key_ok) ? (int)r->spec :
+		selector_match(d, node, attr_ptr(d, r->sel_off),
+			      s_len(attr_ptr(d, r->sel_off)), key_ok);
 	if (spec < 0)
 		return;
 	if (nrule_matches < RULE_MATCH_MAX) {

+ 3 - 0
browser_html.h

@@ -134,7 +134,9 @@ struct br_decl {
 struct br_rule {
 	unsigned int sel_off;       /* selector text in the attr arena */
 	unsigned int key_off;       /* rightmost compound, for a fast prefilter */
+	unsigned short spec;        /* selector specificity */
 	unsigned char nd;
+	unsigned char single;       /* one compound, no ancestor walk needed */
 	struct br_decl d[BR_DECLS];
 };
 
@@ -155,6 +157,7 @@ struct br_doc {
 	 * single table: the last definition wins, which matches :root. */
 	unsigned int var_name[BR_MAX_VARS];
 	unsigned int var_val[BR_MAX_VARS];
+	unsigned short var_slot[1024];
 	unsigned int nvars;
 	/* Rule index: rightmost class/id/tag -> linked list of rule indices. */
 	unsigned short rule_next[BR_MAX_RULES];

+ 134 - 14
dhcp.c

@@ -32,6 +32,19 @@ static unsigned char rxbuf[1520];
 static int have_info;
 static unsigned char last_server[4];
 static unsigned int last_lease;
+/* Number of UDP port-68 datagrams seen in the current dhcp_run(), regardless
+ * of whether dhcp_parse() liked them.  Printed on failure so netmon can tell
+ * "no reply at all" from "reply arrived but was rejected". */
+static unsigned int dhcp_rx68;
+/* Rejection breakdown for the same window. */
+static unsigned int dhcp_frames, dhcp_udp, dhcp_other_udp, dhcp_not_udp;
+static unsigned int dhcp_p68, dhcp_short, dhcp_bad_magic, dhcp_bad_xid;
+static unsigned int dhcp_bad_mac;
+static unsigned int dhcp_first_sport, dhcp_first_dport;
+static unsigned int dhcp_first_xid;
+static unsigned char dhcp_first_src[4], dhcp_first_dst[4];
+static unsigned char dhcp_first_chaddr[6];
+static int dhcp_have_udp, dhcp_have_p68;
 
 static void put_ip(const unsigned char *ip)
 {
@@ -72,6 +85,14 @@ static void put_dec(unsigned int v)
 		io_putchar(b[--i]);
 }
 
+static void put_hex2(unsigned int v)
+{
+	static const char hex[] = "0123456789ABCDEF";
+
+	io_putchar(hex[(v >> 4) & 0xF]);
+	io_putchar(hex[v & 0xF]);
+}
+
 static unsigned int rd32be(const unsigned char *p)
 {
 	return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
@@ -156,20 +177,56 @@ static int dhcp_parse(const unsigned char *frame, unsigned int len,
 	unsigned char s_ip[4], d_ip[4];
 	unsigned int sport, dport, plen, i, o;
 
-	if (!net_udp_parse(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen))
+	dhcp_frames++;
+	if (!net_udp_parse_any(frame, len, s_ip, d_ip, &sport, &dport, &p, &plen)) {
+		dhcp_not_udp++;
 		return 0;
-	if (dport != DHCP_CLIENT_PORT)
+	}
+	dhcp_udp++;
+	if (!dhcp_have_udp) {
+		dhcp_have_udp = 1;
+		dhcp_first_sport = sport;
+		dhcp_first_dport = dport;
+		for (i = 0; i < 4; i++) {
+			dhcp_first_src[i] = s_ip[i];
+			dhcp_first_dst[i] = d_ip[i];
+		}
+	}
+	if (dport != DHCP_CLIENT_PORT) {
+		dhcp_other_udp++;
 		return 0;
-	if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY)
+	}
+	dhcp_rx68++;
+	dhcp_p68++;
+	if (!dhcp_have_p68) {
+		dhcp_have_p68 = 1;
+		dhcp_first_xid = 0;
+		for (i = 0; i < 6; i++)
+			dhcp_first_chaddr[i] = 0;
+		if (plen >= 34) {
+			dhcp_first_xid = rd32be(p + 4);
+			for (i = 0; i < 6; i++)
+				dhcp_first_chaddr[i] = p[28 + i];
+		}
+	}
+	if (plen < DHCP_OPT_OFF || p[0] != DHCP_OP_REPLY) {
+		dhcp_short++;
 		return 0;
+	}
 	if (p[DHCP_MAGIC_OFF] != 0x63 || p[DHCP_MAGIC_OFF + 1] != 0x82 ||
-	    p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63)
+	    p[DHCP_MAGIC_OFF + 2] != 0x53 || p[DHCP_MAGIC_OFF + 3] != 0x63) {
+		dhcp_bad_magic++;
 		return 0;
-	if (rd32be(p + 4) != xid)
+	}
+	if (rd32be(p + 4) != xid) {
+		dhcp_bad_xid++;
 		return 0;
+	}
 	for (i = 0; i < 6; i++) {
-		if (p[28 + i] != my_mac[i])
+		if (p[28 + i] != my_mac[i]) {
+			dhcp_bad_mac++;
 			return 0;
+		}
 	}
 
 	*type = 0;
@@ -277,6 +334,55 @@ static int dhcp_wait(const struct netdev *nd, unsigned int xid,
 	return -1;
 }
 
+static void dhcp_dump_stats(unsigned int xid)
+{
+	unsigned int i;
+
+	io_putstr("dhcp: seen ");
+	put_dec(dhcp_frames);
+	io_putstr(" frames, udp ");
+	put_dec(dhcp_udp);
+	io_putstr(" (p68 ");
+	put_dec(dhcp_p68);
+	io_putstr(", other port ");
+	put_dec(dhcp_other_udp);
+	io_putstr("), notudp ");
+	put_dec(dhcp_not_udp);
+	io_putstr(", short ");
+	put_dec(dhcp_short);
+	io_putstr(", magic ");
+	put_dec(dhcp_bad_magic);
+	io_putstr(", xid ");
+	put_dec(dhcp_bad_xid);
+	io_putstr(", mac ");
+	put_dec(dhcp_bad_mac);
+	io_putchar('\n');
+	if (dhcp_have_udp) {
+		io_putstr("dhcp: first udp ");
+		put_dec(dhcp_first_sport);
+		io_putstr("->");
+		put_dec(dhcp_first_dport);
+		io_putstr(" from ");
+		put_ip(dhcp_first_src);
+		io_putstr(" to ");
+		put_ip(dhcp_first_dst);
+		io_putchar('\n');
+	}
+	if (dhcp_have_p68) {
+		io_putstr("dhcp: first p68 xid 0x");
+		io_puthex(dhcp_first_xid);
+		io_putstr(" want 0x");
+		io_puthex(xid);
+		io_putstr(" chaddr ");
+		for (i = 0; i < 6; i++) {
+			if (i)
+				io_putchar(':');
+			put_hex2(dhcp_first_chaddr[i]);
+		}
+		io_putchar('\n');
+	}
+}
+
 int dhcp_run(unsigned int timeout_ms)
 {
 	static const unsigned char bcast_mac[6] = {
@@ -285,7 +391,7 @@ int dhcp_run(unsigned int timeout_ms)
 	static const unsigned char bcast_ip[4] = { 255, 255, 255, 255 };
 	static const unsigned char zero_ip[4] = { 0, 0, 0, 0 };
 	const struct netdev *nd = netdev_active();
-	unsigned char my_mac[6], want_ip[4], current[4];
+	unsigned char my_mac[6], current[4];
 	unsigned char yiaddr[4], mask[4], gw[4], dns[4], server[4];
 	unsigned int xid, start, deadline, tries;
 	unsigned int lease = 0;
@@ -294,15 +400,23 @@ int dhcp_run(unsigned int timeout_ms)
 		return -2;
 	nd->get_mac(my_mac);
 	net_get_ip(current);
-	/* Try to get the address we would use anyway (static default or the
-	 * previous lease): most routers honour option 50. */
-	for (tries = 0; tries < 4; tries++)
-		want_ip[tries] = current[tries];
 
 	xid = (get_ticks_ms() << 8) ^ ((unsigned int)my_mac[3] << 16) ^
 	      ((unsigned int)my_mac[4] << 8) ^ my_mac[5];
 	start = get_ticks_ms();
 	deadline = start + timeout_ms;
+	dhcp_rx68 = 0;
+	dhcp_frames = dhcp_udp = dhcp_other_udp = dhcp_not_udp = 0;
+	dhcp_p68 = dhcp_short = dhcp_bad_magic = dhcp_bad_xid = 0;
+	dhcp_bad_mac = 0;
+	dhcp_have_udp = dhcp_have_p68 = 0;
+
+	/* Do not share the receiver with a raw net command or a stuck
+	 * netkexec transfer: both would consume each other's replies. */
+	if (netdev_in_use()) {
+		io_putstr("dhcp: NIC is busy (see ps, kill the other command)\n");
+		return -4;
+	}
 
 	netdev_acquire();
 	io_putstr("dhcp: DISCOVER\n");
@@ -314,7 +428,7 @@ int dhcp_run(unsigned int timeout_ms)
 		if ((int)(wait_until - deadline) > 0)
 			wait_until = deadline;
 		dhcp_build(DHCP_DISCOVER, my_mac, xid,
-			   get_ticks_ms() - start, want_ip, 0);
+			   get_ticks_ms() - start, 0, 0);
 		if (net_udp_send_from(zero_ip, bcast_mac, bcast_ip,
 				      DHCP_CLIENT_PORT, DHCP_SERVER_PORT,
 				      msg, DHCP_MSG_LEN) != 0) {
@@ -329,9 +443,12 @@ int dhcp_run(unsigned int timeout_ms)
 	}
 	if (tries == 3) {
 		netdev_release();
-		io_putstr("dhcp: no server, keeping ");
+		io_putstr("dhcp: no server (rx ");
+		put_dec(dhcp_rx68);
+		io_putstr("), keeping ");
 		put_ip(current);
 		io_putchar('\n');
+		dhcp_dump_stats(xid);
 		return -1;
 	}
 
@@ -370,9 +487,12 @@ int dhcp_run(unsigned int timeout_ms)
 	}
 	if (tries == 3) {
 		netdev_release();
-		io_putstr("dhcp: no ACK, keeping ");
+		io_putstr("dhcp: no ACK (rx ");
+		put_dec(dhcp_rx68);
+		io_putstr("), keeping ");
 		put_ip(current);
 		io_putchar('\n');
+		dhcp_dump_stats(xid);
 		return -1;
 	}
 

Разница между файлами не показана из-за своего большого размера
+ 793 - 48
e1000e.c


+ 1311 - 0
ehci.c

@@ -0,0 +1,1311 @@
+/* EHCI (USB 2.0) host controller driver.
+ *
+ * Scope: high-speed devices on the root ports.  The EHCI controller is paired
+ * with UHCI/OHCI companions on most chipsets; low/full-speed devices are left
+ * to those controllers (the hardware routes them back when a port reset shows
+ * a non-high-speed device).  usb.c enumerates every controller type, so a
+ * high-speed USB Ethernet dongle on EHCI and a full-speed mouse/keyboard on a
+ * companion both work.
+ *
+ * The schedule is polled (no IRQs), like the UHCI/OHCI drivers.  All control,
+ * bulk and interrupt endpoints live in the asynchronous schedule: a circular
+ * QH list with the control QH after the reclamation head.  Interrupt endpoints
+ * are retried every microframe rather than through the periodic list; for a
+ * handful of boot HID devices the extra NAK traffic is negligible and it keeps
+ * the schedule simple.
+ *
+ * Descriptors (frame list, QHs, qTDs, bounce buffers) come from the page
+ * allocator, so the driver does not grow the kernel BSS. */
+
+#include "ehci.h"
+#include "io.h"
+#include "mm.h"
+#include "paging.h"
+#include "pci.h"
+#include "serial.h"
+#include "timer.h"
+
+#define EHCI_DEBUG 0
+
+/* ---- capability / operational register offsets (bytes) ----------------- */
+#define EHCI_CAP_CAPLENGTH	0x00
+#define EHCI_CAP_HCSPARAMS	0x04
+#define EHCI_CAP_HCCPARAMS	0x08
+
+#define EHCI_OP_USBCMD		0x00
+#define EHCI_OP_USBSTS		0x04
+#define EHCI_OP_USBINTR		0x08
+#define EHCI_OP_FRINDEX		0x0C
+#define EHCI_OP_CTRLDSSEGMENT	0x10
+#define EHCI_OP_PERIODICLIST	0x14
+#define EHCI_OP_ASYNCLIST	0x18
+#define EHCI_OP_CONFIGFLAG	0x40
+#define EHCI_OP_PORTSC		0x44
+
+/* USBCMD */
+#define CMD_RUN			0x00000001u
+#define CMD_RESET		0x00000002u
+#define CMD_PSE			0x00000010u
+#define CMD_ASE			0x00000020u
+#define CMD_ITC_8		0x00080000u
+
+/* USBSTS */
+#define STS_INT			0x00000001u
+#define STS_ERR			0x00000002u
+#define STS_PCD			0x00000004u
+#define STS_FLR			0x00000008u
+#define STS_FATAL		0x00000010u
+#define STS_IAA			0x00000020u
+#define STS_HALT		0x00001000u
+#define STS_RECL		0x00002000u
+#define STS_PSS			0x00004000u
+#define STS_ASS			0x00008000u
+
+/* PORTSC */
+#define PORT_CONNECT		0x00000001u
+#define PORT_CSC		0x00000002u
+#define PORT_PE			0x00000004u
+#define PORT_PEC		0x00000008u
+#define PORT_OC			0x00000010u
+#define PORT_OCC		0x00000020u
+#define PORT_RESUME		0x00000040u
+#define PORT_SUSPEND		0x00000080u
+#define PORT_RESET		0x00000100u
+#define PORT_LS_MASK		0x00000C00u
+#define PORT_POWER		0x00001000u
+#define PORT_OWNER		0x00002000u
+#define PORT_RWC		(PORT_CSC | PORT_PEC | PORT_OCC)
+#define PORT_USB11(ps)		(((ps) & PORT_LS_MASK) == (1u << 10))
+
+/* qTD token bits (EHCI 3.5.3). */
+#define QTD_ACTIVE		0x00000080u
+#define QTD_HALT		0x00000040u
+#define QTD_DBE			0x00000020u
+#define QTD_BABBLE		0x00000010u
+#define QTD_XACT		0x00000008u
+#define QTD_MMF			0x00000004u
+#define QTD_PING		0x00000001u
+#define QTD_ERRORS		(QTD_HALT | QTD_DBE | QTD_BABBLE | \
+				 QTD_XACT | QTD_MMF)
+#define QTD_TOGGLE		0x80000000u
+#define QTD_PID_OUT		0u
+#define QTD_PID_IN		1u
+#define QTD_PID_SETUP		2u
+#define QTD_CERR_3		(3u << 10)
+#define QTD_LEN(tok)		(((tok) >> 16) & 0x7FFFu)
+
+/* QH endpoint characteristics (EHCI 3.6). */
+#define QH_DEVADDR(a)		((a) & 0x7Fu)
+#define QH_ENDPT(e)		(((e) & 0x0Fu) << 8)
+#define QH_EPS_HIGH		(2u << 12)
+#define QH_DTC			(1u << 14)
+#define QH_HEAD			(1u << 15)
+#define QH_MPS(m)		(((m) & 0x7FFu) << 16)
+#define QH_CTRL_EP		(1u << 27)
+#define QH_RL(n)		(((n) & 7u) << 28)
+
+/* QH endpoint capabilities. */
+#define QH_MULT(n)		(((n) & 3u) << 30)
+
+/* Link pointers: bit 0 terminates, bits 2:1 select the structure type. */
+#define QH_LIST_END		1u
+#define QH_NEXT(a)		(((a) & ~0x1Fu) | 0x2u)
+
+#define EHCI_MAX_QH		(4 + USB_HCD_INT_SLOTS)
+#define EHCI_QH_STRIDE		128
+#define EHCI_QTD_STRIDE		64
+
+struct ehci_qh {
+	volatile unsigned int next;
+	volatile unsigned int info1;
+	volatile unsigned int info2;
+	volatile unsigned int current;
+	volatile unsigned int qtd_next;
+	volatile unsigned int alt_next;
+	volatile unsigned int token;
+	volatile unsigned int buf[5];
+	volatile unsigned int buf_hi[5];   /* 64-bit addressing extension */
+} __attribute__((aligned(32)));
+
+struct ehci_qtd {
+	volatile unsigned int next;
+	volatile unsigned int alt_next;
+	volatile unsigned int token;
+	volatile unsigned int buf[5];
+	volatile unsigned int buf_hi[5];   /* 64-bit addressing extension */
+} __attribute__((aligned(32)));
+
+struct ehci_bulk {
+	struct ehci_qh *qh;
+	struct ehci_qtd *qtd;
+	struct ehci_qtd *zqtd;      /* zero-length packet, bulk OUT only */
+	unsigned int addr;
+	unsigned int ep;
+	unsigned int mps;
+	unsigned int buf_len;
+	unsigned int toggle;
+	int armed;
+};
+
+struct ehci_int {
+	struct ehci_qh *qh;
+	struct ehci_qtd *qtd;
+	unsigned char *buf;
+	unsigned int mps;
+	unsigned int buf_len;
+	unsigned int toggle;
+	int active;
+};
+
+struct ehci_ctrl {
+	unsigned int base;
+	unsigned int cap_len;
+	unsigned int bus, dev, func;
+	unsigned int n_ports;
+	int ppc;
+	int last_port;
+	unsigned int hcs;
+	unsigned int hcc;
+
+	unsigned int frame_list;
+	unsigned int desc_page;
+	unsigned int data_page;
+
+	struct ehci_qh *head;
+	struct ehci_qh *ctl_qh;
+	struct ehci_qh *bin_qh;
+	struct ehci_qh *bout_qh;
+	struct ehci_qh *int_qh[USB_HCD_INT_SLOTS];
+
+	struct ehci_qtd *ctl_qtd[3];
+	struct ehci_qtd *bin_qtd;
+	struct ehci_qtd *bout_qtd;
+	struct ehci_qtd *bout_zqtd;
+	struct ehci_qtd *int_qtd[USB_HCD_INT_SLOTS];
+
+	unsigned char *setup;
+	unsigned char *rx_buf;
+	unsigned char *int_buf[USB_HCD_INT_SLOTS];
+
+	struct ehci_bulk bin;
+	struct ehci_bulk bout;
+	struct ehci_int ints[USB_HCD_INT_SLOTS];
+
+	int ctl_busy;
+	int recovering;
+	unsigned char addr_port[128];
+};
+
+static struct ehci_ctrl ctrls[USB_HCD_MAX_CTRL];
+static unsigned int n_ctrls;
+static struct ehci_int *slot_int[USB_HCD_INT_SLOTS];
+
+static int ehci_start(struct ehci_ctrl *c);
+
+/* ---- debug ------------------------------------------------------------- */
+#if EHCI_DEBUG
+static void dbg(const char *s)
+{
+	serial_puts(SERIAL_COM1, s);
+}
+#else
+static void dbg(const char *s) { (void)s; }
+#endif
+
+static unsigned int rd32(unsigned int addr)
+{
+	return *(volatile unsigned int *)addr;
+}
+
+static void wr32(unsigned int addr, unsigned int val)
+{
+	*(volatile unsigned int *)addr = val;
+}
+
+static unsigned int op_reg(struct ehci_ctrl *c, unsigned int off)
+{
+	return c->base + c->cap_len + off;
+}
+
+static unsigned int port_reg(struct ehci_ctrl *c, unsigned int port)
+{
+	return c->base + c->cap_len + EHCI_OP_PORTSC + port * 4u;
+}
+
+static void delay_ms(unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
+
+static void zero_region(unsigned int addr, unsigned int len)
+{
+	unsigned int *p = (unsigned int *)addr;
+	while (len >= 4) {
+		*p++ = 0;
+		len -= 4;
+	}
+}
+
+static void zero_ctrl(struct ehci_ctrl *c)
+{
+	unsigned char *p = (unsigned char *)c;
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*c); i++)
+		p[i] = 0;
+}
+
+/* ---- descriptor helpers ------------------------------------------------ */
+static void qh_init(struct ehci_qh *qh)
+{
+	unsigned int i;
+
+	qh->info1 = 0;
+	qh->info2 = 0;
+	qh->current = 0;
+	qh->qtd_next = QH_LIST_END;
+	qh->alt_next = QH_LIST_END;
+	qh->token = QTD_HALT;
+	for (i = 0; i < 5; i++) {
+		qh->buf[i] = 0;
+		qh->buf_hi[i] = 0;
+	}
+}
+
+static void qh_configure(struct ehci_qh *qh, unsigned int addr,
+			 unsigned int ep, unsigned int mps, int control)
+{
+	qh->info1 = QH_DEVADDR(addr) | QH_ENDPT(ep) | QH_EPS_HIGH |
+		    QH_MPS(mps) | QH_RL(4);
+	if (control)
+		qh->info1 |= QH_CTRL_EP | QH_DTC;
+	qh->info2 = QH_MULT(1);
+}
+
+static void qh_arm(struct ehci_qh *qh, struct ehci_qtd *qtd)
+{
+	__asm__ volatile ("" ::: "memory");
+	qh->qtd_next = (unsigned int)qtd;
+	qh->alt_next = QH_LIST_END;
+	qh->token &= (QTD_TOGGLE | QTD_PING);
+	__asm__ volatile ("" ::: "memory");
+}
+
+static void qh_disarm(struct ehci_qh *qh)
+{
+	qh->qtd_next = QH_LIST_END;
+	qh->token = (qh->token & (QTD_TOGGLE | QTD_PING)) | QTD_HALT;
+	__asm__ volatile ("" ::: "memory");
+}
+
+/* Fill one qTD.  Up to five 4 KB pages are described (all current callers
+   are well below that); the C_PAGE field starts at zero. */
+static void qtd_fill(struct ehci_qtd *qtd, const void *buf, unsigned int len,
+		     unsigned int pid, int toggle)
+{
+	unsigned int addr = (unsigned int)buf;
+	unsigned int left, chunk, i;
+
+	qtd->next = QH_LIST_END;
+	qtd->alt_next = QH_LIST_END;
+	for (i = 0; i < 5; i++) {
+		qtd->buf[i] = 0;
+		qtd->buf_hi[i] = 0;
+	}
+	qtd->token = ((len & 0x7FFFu) << 16) |
+		     ((pid & 3u) << 8) | QTD_CERR_3 | QTD_ACTIVE |
+		     (toggle ? QTD_TOGGLE : 0);
+
+	if (!len)
+		return;
+
+	qtd->buf[0] = addr;
+	left = len;
+	chunk = 0x1000u - (addr & 0xFFFu);
+	if (chunk > left)
+		chunk = left;
+	left -= chunk;
+	addr += chunk;
+
+	i = 1;
+	while (left && i < 5) {
+		qtd->buf[i++] = addr & 0xFFFFF000u;
+		chunk = (left > 0x1000u) ? 0x1000u : left;
+		left -= chunk;
+		addr += chunk;
+	}
+}
+
+static int qtd_error(unsigned int token)
+{
+	return (token & QTD_ERRORS) != 0;
+}
+
+/* Wait for the qTD to retire.  If early is non-NULL and has halted, stop
+   waiting immediately (a control data-stage STALL leaves the status qTD
+   armed forever). */
+static int qtd_wait(struct ehci_qtd *qtd, struct ehci_qtd *early,
+		    unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (qtd->token & QTD_ACTIVE) {
+		if (early && !(early->token & QTD_ACTIVE) &&
+		    qtd_error(early->token))
+			return -1;
+		if (get_ticks_ms() - t0 >= ms)
+			return -1;
+		__asm__ volatile ("pause");
+	}
+	return 0;
+}
+
+/* ---- bulk endpoints ---------------------------------------------------- */
+static void bulk_stop(struct ehci_bulk *b)
+{
+	if (b->qh)
+		qh_disarm(b->qh);
+	if (b->qtd)
+		b->qtd->token = 0;
+	if (b->zqtd)
+		b->zqtd->token = 0;
+	b->armed = 0;
+}
+
+static void bulk_reset(struct ehci_bulk *b)
+{
+	bulk_stop(b);
+	b->addr = 0xFFFFFFFFu;
+	b->ep = 0;
+	b->mps = 0;
+	b->buf_len = 0;
+	b->toggle = 0;
+}
+
+static void bulk_configure(struct ehci_bulk *b, unsigned int addr,
+			   unsigned int ep, unsigned int mps,
+			   unsigned int buf_len)
+{
+	if (b->addr == addr && b->ep == ep && b->mps == mps)
+		return;
+	bulk_stop(b);
+	b->addr = addr;
+	b->ep = ep;
+	b->mps = mps;
+	b->buf_len = buf_len;
+	b->toggle = 0;                  /* first packet is DATA0 */
+	qh_init(b->qh);                 /* keeps the link pointer out of it */
+	qh_configure(b->qh, addr, ep, mps, 0);
+	b->qh->token = 0;               /* DATA0, not halted */
+	b->armed = 0;
+}
+
+static void bulk_arm(struct ehci_bulk *b)
+{
+	b->qh->token = (b->qh->token & QTD_PING) |
+		       (b->toggle ? QTD_TOGGLE : 0);
+	qh_arm(b->qh, b->qtd);
+	b->armed = 1;
+}
+
+static int ehci_bulk_out(unsigned int ctrl, unsigned int addr,
+			 unsigned int ep, unsigned int mps,
+			 const unsigned char *buf, unsigned int len)
+{
+	struct ehci_ctrl *c;
+	struct ehci_bulk *b;
+	struct ehci_qtd *qtd, *zqtd, *last;
+	unsigned int token, ztoken = 0;
+	int zlp;
+
+	if (ctrl >= n_ctrls || !buf || !len || len > 0x7FFFu)
+		return -1;
+	c = &ctrls[ctrl];
+	b = &c->bout;
+	if (!b->qh || !b->qtd || !b->zqtd)
+		return -1;
+	if (mps < 1)
+		mps = 512;
+	if (mps > 1024)
+		mps = 1024;
+
+	bulk_configure(b, addr, ep, mps, 0);
+	bulk_stop(b);
+	qtd = b->qtd;
+	zqtd = b->zqtd;
+	qtd_fill(qtd, buf, len, QTD_PID_OUT, 0);
+
+	/* A bulk transfer that ends on a max-packet boundary needs a
+	   zero-length packet to mark the end (USB 2.0 5.8.3). */
+	zlp = (len % mps) == 0;
+	if (zlp) {
+		qtd_fill(zqtd, 0, 0, QTD_PID_OUT, 0);
+		qtd->next = (unsigned int)zqtd;
+		last = zqtd;
+	} else {
+		last = qtd;
+	}
+	bulk_arm(b);
+
+	if (qtd_wait(last, qtd, 2000) < 0) {
+		bulk_stop(b);
+		return -1;
+	}
+
+	token = qtd->token;
+	if (zlp)
+		ztoken = last->token;
+	bulk_stop(b);
+	if ((token & QTD_ACTIVE) || qtd_error(token) ||
+	    (zlp && ((ztoken & QTD_ACTIVE) || qtd_error(ztoken))))
+		return -1;
+
+	{
+		unsigned int packets = (len + mps - 1) / mps;
+
+		if (zlp)
+			packets++;
+		b->toggle ^= packets & 1u;
+	}
+	return 0;
+}
+
+/* One page of bounce buffer; the caller's buffer stays stable while the
+   completed transfer is consumed (see usb_hcd.h). */
+#define EHCI_RX_MAX	2048u
+
+static int ehci_bulk_in(unsigned int ctrl, unsigned int addr,
+			unsigned int ep, unsigned int mps,
+			unsigned char *buf, unsigned int len)
+{
+	struct ehci_ctrl *c;
+	struct ehci_bulk *b;
+	unsigned int token, raw, packets, actual, i;
+
+	if (ctrl >= n_ctrls || !buf || !len)
+		return -1;
+	c = &ctrls[ctrl];
+	b = &c->bin;
+	if (!b->qh || !b->qtd || !c->rx_buf)
+		return -1;
+	if (mps < 1)
+		mps = 512;
+	if (mps > 1024)
+		mps = 1024;
+
+	bulk_configure(b, addr, ep, mps, EHCI_RX_MAX);
+
+	if (!b->armed) {
+		qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
+		bulk_arm(b);
+		return 0;
+	}
+
+	token = b->qtd->token;
+	if (token & QTD_ACTIVE)
+		return 0;
+
+	b->armed = 0;
+	if (qtd_error(token)) {
+		bulk_stop(b);
+		return -1;
+	}
+
+	raw = QTD_LEN(token) <= b->buf_len ?
+		b->buf_len - QTD_LEN(token) : 0;
+	actual = raw;
+	if (actual > len)
+		actual = len;
+	for (i = 0; i < actual; i++)
+		buf[i] = c->rx_buf[i];
+
+	/* A bulk IN transfer ends with a short packet; a zero-length
+	   packet also counts as one successful transaction. */
+	packets = raw ? (raw + b->mps - 1) / b->mps : 1;
+	b->toggle ^= packets & 1u;
+
+	qh_disarm(b->qh);
+	qtd_fill(b->qtd, c->rx_buf, b->buf_len, QTD_PID_IN, 0);
+	bulk_arm(b);
+	return (int)actual;
+}
+
+/* ---- interrupt-IN endpoints ------------------------------------------- */
+static void int_arm(struct ehci_int *in)
+{
+	in->qh->token = (in->qh->token & QTD_PING) |
+			(in->toggle ? QTD_TOGGLE : 0);
+	qh_arm(in->qh, in->qtd);
+}
+
+static void ehci_int_in_stop(unsigned int slot)
+{
+	struct ehci_int *in;
+
+	if (slot >= USB_HCD_INT_SLOTS)
+		return;
+	in = slot_int[slot];
+	if (!in)
+		return;
+	in->active = 0;
+	if (in->qh)
+		qh_disarm(in->qh);
+	if (in->qtd)
+		in->qtd->token = 0;
+	slot_int[slot] = 0;
+}
+
+static int ehci_int_in_start(unsigned int ctrl, unsigned int slot,
+			     unsigned int addr, unsigned int ep,
+			     unsigned int mps, int low_speed)
+{
+	struct ehci_ctrl *c;
+	struct ehci_int *in;
+
+	if (ctrl >= n_ctrls || slot >= USB_HCD_INT_SLOTS || low_speed)
+		return -1;
+	c = &ctrls[ctrl];
+	in = &c->ints[slot];
+	if (!in->qh || !in->qtd || !in->buf)
+		return -1;
+
+	ehci_int_in_stop(slot);
+
+	in->mps = mps ? mps : 8;
+	if (in->mps > 1024)
+		in->mps = 1024;
+	in->buf_len = in->mps > 64 ? 64 : in->mps;
+
+	qh_init(in->qh);
+	in->qh->info1 = QH_DEVADDR(addr) | QH_ENDPT(ep) | QH_EPS_HIGH |
+			QH_MPS(in->mps) | QH_RL(4);
+	in->qh->info2 = QH_MULT(1);
+	in->qh->token = 0;
+	in->toggle = 0;                 /* first report is DATA0 */
+
+	qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
+	int_arm(in);
+	in->active = 1;
+	slot_int[slot] = in;
+	return 0;
+}
+
+static int ehci_int_in_poll(unsigned int slot, unsigned char *buf,
+			    unsigned int len)
+{
+	struct ehci_int *in;
+	unsigned int token, raw, packets, actual, i;
+
+	if (slot >= USB_HCD_INT_SLOTS)
+		return 0;
+	in = slot_int[slot];
+	if (!in || !in->active)
+		return 0;
+
+	token = in->qtd->token;
+	if (token & QTD_ACTIVE)
+		return 0;
+
+	if (qtd_error(token)) {
+		qh_disarm(in->qh);
+		qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
+		int_arm(in);
+		return -1;
+	}
+
+	raw = QTD_LEN(token) <= in->buf_len ?
+		in->buf_len - QTD_LEN(token) : 0;
+	actual = raw;
+	if (actual > len)
+		actual = len;
+	for (i = 0; i < actual; i++)
+		buf[i] = in->buf[i];
+
+	packets = raw ? (raw + in->mps - 1) / in->mps : 1;
+	in->toggle ^= packets & 1u;
+
+	qh_disarm(in->qh);
+	qtd_fill(in->qtd, in->buf, in->buf_len, QTD_PID_IN, 0);
+	int_arm(in);
+	return (int)actual;
+}
+
+/* ---- control transfers ------------------------------------------------- */
+static int ehci_control(struct ehci_ctrl *c, unsigned int addr,
+			unsigned int mps, int low_speed,
+			const unsigned char *setup, unsigned char *data,
+			unsigned int len, int dir_in)
+{
+	struct ehci_qh *qh;
+	struct ehci_qtd *q0, *q1, *q2, *last, *early;
+	unsigned int i, actual = 0, residual;
+
+	(void)mps;
+	if (!c->ctl_qh || !c->setup || !setup || c->ctl_busy || low_speed)
+		return -1;
+	if (len && !data)
+		return -1;
+
+	qh = c->ctl_qh;
+	q0 = c->ctl_qtd[0];
+	q1 = c->ctl_qtd[1];
+	q2 = c->ctl_qtd[2];
+	c->ctl_busy = 1;
+
+	for (i = 0; i < 8; i++)
+		c->setup[i] = setup[i];
+
+	qh_init(qh);
+	qh_configure(qh, addr, 0, 64, 1);
+	qh->token = 0;                  /* control toggles come from the qTDs */
+
+	qtd_fill(q0, c->setup, 8, QTD_PID_SETUP, 0);
+	if (len) {
+		qtd_fill(q1, data, len,
+			 dir_in ? QTD_PID_IN : QTD_PID_OUT, 1);
+		qtd_fill(q2, 0, 0,
+			 dir_in ? QTD_PID_OUT : QTD_PID_IN, 1);
+		q0->next = (unsigned int)q1;
+		q1->next = (unsigned int)q2;
+		last = q2;
+		early = q1;
+	} else {
+		qtd_fill(q2, 0, 0, QTD_PID_IN, 1);
+		q0->next = (unsigned int)q2;
+		last = q2;
+		early = q0;
+	}
+	q2->next = QH_LIST_END;
+
+	qh_arm(qh, q0);
+	if (qtd_wait(last, early, 2000) < 0) {
+		qh_disarm(qh);
+		delay_ms(1);
+		c->ctl_busy = 0;
+		return -1;
+	}
+	qh_disarm(qh);
+
+	if (qtd_error(q0->token) || qtd_error(q2->token) ||
+	    (len && qtd_error(q1->token))) {
+		c->ctl_busy = 0;
+		return -1;
+	}
+
+	if (len && dir_in) {
+		residual = QTD_LEN(q1->token);
+		actual = residual >= len ? 0 : len - residual;
+	} else if (len) {
+		actual = len;
+	}
+
+	/* The device changes address after the status stage.  Remember which
+	   root port it came from so a port reset can drop this controller's
+	   bulk state without touching an unrelated port. */
+	if (addr == 0 && setup[0] == 0x00 && setup[1] == 0x05) {
+		unsigned int new_addr = (unsigned int)setup[2] |
+					((unsigned int)setup[3] << 8);
+		if (new_addr > 0 && new_addr < 128 && c->last_port >= 0)
+			c->addr_port[new_addr] = (unsigned char)c->last_port;
+	}
+
+	c->ctl_busy = 0;
+	return (int)actual;
+}
+
+static int ehci_control_transfer(unsigned int ctrl, unsigned int addr,
+				 unsigned int mps, int low_speed,
+				 const unsigned char *setup,
+				 unsigned char *data, unsigned int len,
+				 int dir_in)
+{
+	if (ctrl >= n_ctrls)
+		return -1;
+	return ehci_control(&ctrls[ctrl], addr, mps, low_speed, setup, data,
+			    len, dir_in);
+}
+
+/* ---- root hub ports ---------------------------------------------------- */
+static void port_endpoints_reset(struct ehci_ctrl *c, unsigned int port)
+{
+	unsigned int i;
+
+	if (c->bin.addr < 128 && c->addr_port[c->bin.addr] == port)
+		bulk_reset(&c->bin);
+	if (c->bout.addr < 128 && c->addr_port[c->bout.addr] == port)
+		bulk_reset(&c->bout);
+	for (i = 0; i < 128; i++)
+		if (c->addr_port[i] == port)
+			c->addr_port[i] = 0xFF;
+}
+
+static unsigned int ehci_port_count(unsigned int ctrl)
+{
+	return ctrl < n_ctrls ? ctrls[ctrl].n_ports : 0;
+}
+
+static int ehci_port_connected(unsigned int ctrl, unsigned int port)
+{
+	struct ehci_ctrl *c;
+	unsigned int ps;
+
+	if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
+		return 0;
+	c = &ctrls[ctrl];
+	ps = rd32(port_reg(c, port));
+	return (ps & PORT_CONNECT) ? 1 : 0;
+}
+
+static int ehci_port_low_speed(unsigned int ctrl, unsigned int port)
+{
+	(void)ctrl;
+	(void)port;
+	return 0;                       /* EHCI only drives high-speed devices */
+}
+
+static int ehci_port_take_change(unsigned int ctrl, unsigned int port)
+{
+	struct ehci_ctrl *c;
+	unsigned int ps;
+
+	if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
+		return 0;
+	c = &ctrls[ctrl];
+	ps = rd32(port_reg(c, port));
+	if (!(ps & PORT_CSC))
+		return 0;
+	wr32(port_reg(c, port),
+	     (ps & ~PORT_RWC) | PORT_CSC | PORT_PEC | PORT_OCC);
+	return 1;
+}
+
+static int ehci_port_reset_once(unsigned int ctrl, unsigned int port)
+{
+	struct ehci_ctrl *c;
+	unsigned int ps, t0, try;
+
+	if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
+		return -1;
+	c = &ctrls[ctrl];
+
+	/* A halted controller ignores port commands; restart it first. */
+	if (rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT) {
+		serial_puts(SERIAL_COM1, "ehci: controller halted, restarting\n");
+		if (ehci_start(c) < 0)
+			return -1;
+	}
+
+	ps = rd32(port_reg(c, port));
+	if (!(ps & PORT_CONNECT))
+		return -1;
+
+	/* Drop any reset/suspend state the firmware left behind.  The port
+	   can be stuck with PR set; a single clear write is not enough on
+	   some PCHs. */
+	for (try = 0; try < 10 && (ps & PORT_RESET); try++) {
+		wr32(port_reg(c, port),
+		     (ps & ~(PORT_RWC | PORT_RESET | PORT_PE |
+			     PORT_SUSPEND | PORT_RESUME)) |
+		     PORT_CSC | PORT_PEC | PORT_OCC);
+		delay_ms(5);
+		ps = rd32(port_reg(c, port));
+	}
+	if ((ps & PORT_RESET) || !(ps & PORT_CONNECT))
+		return -2;
+
+	/* The port may still be owned by the companion controller.  A
+	   low/full-speed device belongs there; a high-speed one can be taken
+	   over by clearing PORT_OWNER (the controller may need more than one
+	   write while the port is enabled). */
+	if (ps & PORT_OWNER) {
+		if (PORT_USB11(ps))
+			return -1;
+		for (try = 0; try < 8 && (ps & PORT_OWNER); try++) {
+			wr32(port_reg(c, port),
+			     (ps & ~(PORT_RWC | PORT_OWNER | PORT_RESET |
+				     PORT_PE | PORT_SUSPEND | PORT_RESUME)) |
+			     PORT_CSC | PORT_PEC | PORT_OCC);
+			delay_ms(5);
+			ps = rd32(port_reg(c, port));
+		}
+		if ((ps & PORT_OWNER) || !(ps & PORT_CONNECT))
+			return -1;
+	}
+
+	port_endpoints_reset(c, port);
+	c->last_port = (int)port;
+
+	/* Clear stale change bits, then assert reset.  PED is cleared while
+	   reset is asserted (like Linux's SetPortFeature(RESET)). */
+	wr32(port_reg(c, port),
+	     (ps & ~(PORT_RWC | PORT_PE | PORT_SUSPEND | PORT_RESUME)) |
+	     PORT_CSC | PORT_PEC | PORT_OCC | PORT_RESET);
+	delay_ms(50);
+
+	/* EHCI expects software to end the reset, but some controllers hold
+	   PR set while the device is still chirping.  Give it up to 200 ms to
+	   self-clear before forcing the write. */
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < 200) {
+		if (!(rd32(port_reg(c, port)) & PORT_RESET))
+			break;
+		delay_ms(1);
+	}
+
+	/* Force completion if the controller still holds PR; retry because
+	   the first write can land while the reset is still in progress. */
+	for (try = 0; try < 20; try++) {
+		ps = rd32(port_reg(c, port));
+		if (!(ps & PORT_RESET))
+			break;
+		wr32(port_reg(c, port),
+		     (ps & ~(PORT_RWC | PORT_RESET | PORT_PE |
+			     PORT_SUSPEND | PORT_RESUME)) |
+		     PORT_CSC | PORT_PEC | PORT_OCC);
+		delay_ms(10);
+	}
+	if (ps & PORT_RESET) {
+		serial_puts(SERIAL_COM1, "ehci: reset stuck ");
+		serial_putdec(SERIAL_COM1, c->bus);
+		serial_putc(SERIAL_COM1, ':');
+		serial_putdec(SERIAL_COM1, c->dev);
+		serial_putc(SERIAL_COM1, '.');
+		serial_putdec(SERIAL_COM1, c->func);
+		serial_puts(SERIAL_COM1, " port ");
+		serial_putdec(SERIAL_COM1, port + 1);
+		serial_puts(SERIAL_COM1, " ps ");
+		serial_puthex(SERIAL_COM1, ps);
+		serial_putc(SERIAL_COM1, '\n');
+		return -2;
+	}
+
+	t0 = get_ticks_ms();
+	while (!(rd32(port_reg(c, port)) & PORT_PE)) {
+		if (get_ticks_ms() - t0 > 200)
+			break;
+	}
+	ps = rd32(port_reg(c, port));
+
+	if (!(ps & PORT_PE)) {
+		if (ps & PORT_OWNER)
+			return -1;
+		if (PORT_USB11(ps)) {
+			/* Low/full speed: make sure the companion owns it. */
+			wr32(port_reg(c, port),
+			     (ps & ~(PORT_RWC | PORT_PE)) | PORT_OWNER |
+			     PORT_CSC | PORT_PEC | PORT_OCC);
+			return -1;
+		}
+		serial_puts(SERIAL_COM1, "ehci: reset no PED ");
+		serial_puthex(SERIAL_COM1, ps);
+		serial_putc(SERIAL_COM1, '\n');
+		return -2;
+	}
+	if (ps & PORT_OWNER)
+		return -1;
+	return 0;
+}
+
+/* A stuck HS port reset on some Intel PCHs only completes after the whole
+   EHCI controller is reset.  Retry the port reset once in that case. */
+static int ehci_port_reset(unsigned int ctrl, unsigned int port)
+{
+	struct ehci_ctrl *c;
+	int r;
+
+	if (ctrl >= n_ctrls || port >= ctrls[ctrl].n_ports)
+		return -1;
+	c = &ctrls[ctrl];
+	r = ehci_port_reset_once(ctrl, port);
+	if (r == -2 && !c->recovering) {
+		c->recovering = 1;
+		serial_puts(SERIAL_COM1,
+			    "ehci: port reset retry after controller reset\n");
+		if (ehci_start(c) == 0)
+			r = ehci_port_reset_once(ctrl, port);
+		else
+			r = -1;
+		c->recovering = 0;
+	}
+	return r < 0 ? -1 : 0;
+}
+
+/* ---- schedule / controller setup -------------------------------------- */
+static int ehci_sched_init(struct ehci_ctrl *c)
+{
+	unsigned char *desc = (unsigned char *)c->desc_page;
+	unsigned int qtd_base = c->desc_page + EHCI_MAX_QH * EHCI_QH_STRIDE;
+	struct ehci_qh *list[EHCI_MAX_QH];
+	unsigned int i;
+
+	c->head = (struct ehci_qh *)(desc + 0 * EHCI_QH_STRIDE);
+	c->ctl_qh = (struct ehci_qh *)(desc + 1 * EHCI_QH_STRIDE);
+	c->bin_qh = (struct ehci_qh *)(desc + 2 * EHCI_QH_STRIDE);
+	c->bout_qh = (struct ehci_qh *)(desc + 3 * EHCI_QH_STRIDE);
+	for (i = 0; i < USB_HCD_INT_SLOTS; i++)
+		c->int_qh[i] = (struct ehci_qh *)(desc +
+					(4 + i) * EHCI_QH_STRIDE);
+
+	c->ctl_qtd[0] = (struct ehci_qtd *)(qtd_base + 0 * EHCI_QTD_STRIDE);
+	c->ctl_qtd[1] = (struct ehci_qtd *)(qtd_base + 1 * EHCI_QTD_STRIDE);
+	c->ctl_qtd[2] = (struct ehci_qtd *)(qtd_base + 2 * EHCI_QTD_STRIDE);
+	c->bin_qtd = (struct ehci_qtd *)(qtd_base + 3 * EHCI_QTD_STRIDE);
+	c->bout_qtd = (struct ehci_qtd *)(qtd_base + 4 * EHCI_QTD_STRIDE);
+	c->bout_zqtd = (struct ehci_qtd *)(qtd_base + 11 * EHCI_QTD_STRIDE);
+	for (i = 0; i < USB_HCD_INT_SLOTS; i++)
+		c->int_qtd[i] = (struct ehci_qtd *)(qtd_base +
+					(5 + i) * EHCI_QTD_STRIDE);
+
+	c->rx_buf = (unsigned char *)c->data_page;
+	c->setup = (unsigned char *)(c->data_page + 2048);
+	for (i = 0; i < USB_HCD_INT_SLOTS; i++)
+		c->int_buf[i] = (unsigned char *)(c->data_page + 2112 +
+						  i * 64);
+
+	for (i = 0; i < 1024; i++)
+		wr32(c->frame_list + i * 4, QH_LIST_END);
+
+	list[0] = c->head;
+	list[1] = c->ctl_qh;
+	list[2] = c->bin_qh;
+	list[3] = c->bout_qh;
+	for (i = 0; i < USB_HCD_INT_SLOTS; i++)
+		list[4 + i] = c->int_qh[i];
+
+	for (i = 0; i < EHCI_MAX_QH; i++) {
+		qh_init(list[i]);
+		list[i]->next = QH_NEXT((unsigned int)
+					list[(i + 1) % EHCI_MAX_QH]);
+	}
+	c->head->info1 = QH_HEAD;
+	c->head->token = QTD_HALT;
+
+	c->bin.qh = c->bin_qh;
+	c->bin.qtd = c->bin_qtd;
+	c->bin.zqtd = 0;
+	c->bin.addr = 0xFFFFFFFFu;
+	c->bout.qh = c->bout_qh;
+	c->bout.qtd = c->bout_qtd;
+	c->bout.zqtd = c->bout_zqtd;
+	c->bout.addr = 0xFFFFFFFFu;
+	for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
+		c->ints[i].qh = c->int_qh[i];
+		c->ints[i].qtd = c->int_qtd[i];
+		c->ints[i].buf = c->int_buf[i];
+		c->ints[i].active = 0;
+	}
+	for (i = 0; i < 128; i++)
+		c->addr_port[i] = 0xFF;
+	c->last_port = -1;
+	return 0;
+}
+
+static void ehci_halt(struct ehci_ctrl *c)
+{
+	unsigned int t0;
+
+	wr32(op_reg(c, EHCI_OP_USBINTR), 0);
+	wr32(op_reg(c, EHCI_OP_USBCMD), 0);
+	t0 = get_ticks_ms();
+	while (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT)) {
+		if (get_ticks_ms() - t0 > 100)
+			break;
+	}
+}
+
+static int ehci_start(struct ehci_ctrl *c)
+{
+	unsigned int v, t0, i;
+
+	/* Stop first, then reset.  HCRESET self-clears. */
+	ehci_halt(c);
+	wr32(op_reg(c, EHCI_OP_USBCMD), CMD_RESET);
+	t0 = get_ticks_ms();
+	while (rd32(op_reg(c, EHCI_OP_USBCMD)) & CMD_RESET) {
+		if (get_ticks_ms() - t0 > 250) {
+			dbg("ehci: reset timeout\n");
+			return -1;
+		}
+	}
+	delay_ms(2);
+
+	if (c->hcc & 1u)                /* 64-bit addressing capable */
+		wr32(op_reg(c, EHCI_OP_CTRLDSSEGMENT), 0);
+	wr32(op_reg(c, EHCI_OP_PERIODICLIST), c->frame_list);
+	wr32(op_reg(c, EHCI_OP_ASYNCLIST), (unsigned int)c->head);
+	wr32(op_reg(c, EHCI_OP_USBINTR), 0);
+	wr32(op_reg(c, EHCI_OP_USBSTS), 0x3Fu);
+
+	wr32(op_reg(c, EHCI_OP_USBCMD), CMD_RUN | CMD_ASE | CMD_ITC_8);
+	__asm__ volatile ("" ::: "memory");
+
+	t0 = get_ticks_ms();
+	while (rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_HALT) {
+		if (get_ticks_ms() - t0 > 100) {
+			dbg("ehci: controller did not start\n");
+			return -1;
+		}
+	}
+
+	/* Take the root ports over from the companion controllers. */
+	wr32(op_reg(c, EHCI_OP_CONFIGFLAG), 1);
+	v = rd32(op_reg(c, EHCI_OP_CONFIGFLAG));
+	if (!(v & 1u))
+		serial_puts(SERIAL_COM1, "ehci: CONFIGFLAG did not stick\n");
+	delay_ms(5);
+
+	if (c->ppc) {
+		for (i = 0; i < c->n_ports; i++) {
+			v = rd32(port_reg(c, i));
+			if (!(v & PORT_POWER))
+				wr32(port_reg(c, i),
+				     (v & ~PORT_RWC) | PORT_POWER);
+		}
+		delay_ms(100);
+	}
+
+	t0 = get_ticks_ms();
+	while (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_ASS)) {
+		if (get_ticks_ms() - t0 > 100)
+			break;
+	}
+	if (!(rd32(op_reg(c, EHCI_OP_USBSTS)) & STS_ASS))
+		dbg("ehci: async schedule not active\n");
+
+	return 0;
+}
+
+/* Best-effort EHCI legacy-support handshake: stop the firmware's SMIs from
+   firing on port changes now that the kernel owns the controller. */
+static void legacy_smi_disable(unsigned int bus, unsigned int dev,
+			       unsigned int func, unsigned int hcc)
+{
+	unsigned int eecp = (hcc >> 8) & 0xFFu;
+	unsigned int guard = 0;
+
+	while (eecp >= 0x40 && guard++ < 16) {
+		unsigned int cap = pci_cfg_read32(bus, dev, func, eecp);
+		unsigned int id = cap & 0xFFu;
+		unsigned int next = (cap >> 8) & 0xFFu;
+
+		if (id == 1) {          /* EHCI Legacy Support */
+			pci_cfg_write32(bus, dev, func, eecp,
+					(cap | (1u << 24)) & ~(1u << 31));
+			cap = pci_cfg_read32(bus, dev, func, eecp + 4);
+			pci_cfg_write32(bus, dev, func, eecp + 4,
+					cap & 0xFFFF0000u);
+			return;
+		}
+		if (!next || next == eecp)
+			return;
+		eecp = next;
+	}
+}
+
+static int ehci_setup_one(unsigned int bus, unsigned int dev,
+			  unsigned int func, unsigned int bar)
+{
+	struct ehci_ctrl *c;
+	unsigned int cap_len, hcs, hcc;
+
+	if (n_ctrls >= USB_HCD_MAX_CTRL)
+		return -1;
+	c = &ctrls[n_ctrls];
+	zero_ctrl(c);
+	c->base = bar;
+	c->bus = bus;
+	c->dev = dev;
+	c->func = func;
+
+	paging_map_mmio(bar, 0x1000);
+
+	if (rd32(bar) == 0xFFFFFFFFu)
+		return -1;
+	cap_len = rd32(bar + EHCI_CAP_CAPLENGTH) & 0xFFu;
+	if (cap_len < 0x20 || cap_len > 0x100)
+		return -1;
+	hcs = rd32(bar + EHCI_CAP_HCSPARAMS);
+	if (hcs == 0xFFFFFFFFu)
+		return -1;
+	c->cap_len = cap_len;
+	c->n_ports = hcs & 0x0Fu;
+	c->ppc = (hcs & 0x10u) ? 1 : 0;
+	c->hcs = hcs;
+	if (!c->n_ports) {
+		dbg("ehci: no root ports\n");
+		return -1;
+	}
+	hcc = rd32(bar + EHCI_CAP_HCCPARAMS);
+	c->hcc = hcc;
+	legacy_smi_disable(bus, dev, func, hcc);
+
+	c->frame_list = mm_alloc_page();
+	c->desc_page = mm_alloc_page();
+	c->data_page = mm_alloc_page();
+	if (!c->frame_list || !c->desc_page || !c->data_page) {
+		dbg("ehci: out of pages\n");
+		goto fail;
+	}
+	zero_region(c->frame_list, 4096);
+	zero_region(c->desc_page, 4096);
+	zero_region(c->data_page, 4096);
+
+	ehci_sched_init(c);
+	if (ehci_start(c) < 0)
+		goto fail;
+
+	serial_puts(SERIAL_COM1, "ehci: controller at ");
+	serial_putdec(SERIAL_COM1, bus);
+	serial_puts(SERIAL_COM1, ":");
+	serial_putdec(SERIAL_COM1, dev);
+	serial_puts(SERIAL_COM1, ".");
+	serial_putdec(SERIAL_COM1, func);
+	serial_puts(SERIAL_COM1, " ports ");
+	serial_putdec(SERIAL_COM1, c->n_ports);
+	serial_puts(SERIAL_COM1, "\n");
+
+	n_ctrls++;
+	return 0;
+
+fail:
+	ehci_halt(c);
+	/* Give the shared ports back to the companion controllers. */
+	wr32(op_reg(c, EHCI_OP_CONFIGFLAG), 0);
+	if (c->frame_list)
+		mm_free_page(c->frame_list);
+	if (c->desc_page)
+		mm_free_page(c->desc_page);
+	if (c->data_page)
+		mm_free_page(c->data_page);
+	zero_ctrl(c);
+	return -1;
+}
+
+static int ehci_init(void)
+{
+	unsigned int bus, dev, func;
+
+	if (n_ctrls)
+		return (int)n_ctrls;
+
+	for (bus = 0; bus < 256 && n_ctrls < USB_HCD_MAX_CTRL; bus++) {
+		for (dev = 0; dev < 32; dev++) {
+			for (func = 0; func < 8; func++) {
+				unsigned int id = pci_cfg_read32(bus, dev, func,
+								 0x00);
+				unsigned int class_rev, bar, bar_hi, cmd;
+
+				if (id == 0xFFFFFFFFu || id == 0)
+					continue;
+				class_rev = pci_cfg_read32(bus, dev, func,
+							   0x08);
+				if ((class_rev >> 16) != 0x0C03u)
+					continue;
+				if (((class_rev >> 8) & 0xFFu) != 0x20u)
+					continue;   /* not EHCI */
+
+				bar = pci_cfg_read32(bus, dev, func, 0x10);
+				if (bar & 1u)
+					continue;   /* register block is MMIO */
+				bar_hi = 0;
+				if (((bar >> 1) & 3u) == 2u)
+					bar_hi = pci_cfg_read32(bus, dev, func,
+								0x14);
+				bar &= 0xFFFFFFF0u;
+				if (!bar || bar_hi)
+					continue;
+
+				cmd = pci_cfg_read32(bus, dev, func, 0x04);
+				pci_cfg_write32(bus, dev, func, 0x04,
+						cmd | 0x0006u);
+
+				ehci_setup_one(bus, dev, func, bar);
+			}
+		}
+	}
+	return (int)n_ctrls;
+}
+
+static unsigned int ehci_count(void)
+{
+	return n_ctrls;
+}
+
+static void ehci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
+		      unsigned int *func, unsigned int *base, int *mmio)
+{
+	struct ehci_ctrl *c = &ctrls[ctrl];
+
+	*bus = c->bus;
+	*dev = c->dev;
+	*func = c->func;
+	*base = c->base;
+	*mmio = 1;
+}
+
+/* Raw PORTSC for `lsusb`; useful when a port is connected but owned by a
+   companion (bit 13) or not powered (bit 12). */
+static void ehci_print_ports(unsigned int ctrl)
+{
+	struct ehci_ctrl *c;
+	unsigned int i, ps, eecp;
+
+	if (ctrl >= n_ctrls)
+		return;
+	c = &ctrls[ctrl];
+	io_putstr("    CMD 0x");
+	io_puthex(rd32(op_reg(c, EHCI_OP_USBCMD)));
+	io_putstr(" STS 0x");
+	io_puthex(rd32(op_reg(c, EHCI_OP_USBSTS)));
+	io_putchar('\n');
+	io_putstr("    CF 0x");
+	io_puthex(rd32(op_reg(c, EHCI_OP_CONFIGFLAG)));
+	io_putstr(" HCC 0x");
+	io_puthex(c->hcc);
+	io_putstr(" HCS 0x");
+	io_puthex(c->hcs);
+	io_putchar('\n');
+	eecp = (c->hcc >> 8) & 0xFFu;
+	if (eecp >= 0x40) {
+		unsigned int leg = pci_cfg_read32(c->bus, c->dev, c->func,
+						 eecp);
+		if ((leg & 0xFFu) == 1u) {
+			io_putstr("    LEGSUP 0x");
+			io_puthex(leg);
+			io_putchar('\n');
+		}
+	}
+
+	for (i = 0; i < c->n_ports; i++) {
+		ps = rd32(port_reg(c, i));
+		io_putstr("    Port ");
+		if (i < 9)
+			io_putchar((char)('1' + i));
+		else
+			io_putchar((char)('A' + i - 9));
+		io_putstr(": 0x");
+		io_puthex(ps);
+		io_putchar('\n');
+	}
+}
+
+const struct usb_hcd_ops usb_ehci_ops = {
+	"EHCI",
+	ehci_init,
+	ehci_count,
+	ehci_info,
+	ehci_control_transfer,
+	ehci_port_count,
+	ehci_port_connected,
+	ehci_port_low_speed,
+	ehci_port_reset,
+	ehci_port_take_change,
+	ehci_int_in_start,
+	ehci_int_in_poll,
+	ehci_int_in_stop,
+	ehci_bulk_out,
+	ehci_bulk_in,
+	ehci_print_ports,
+};

+ 11 - 0
ehci.h

@@ -0,0 +1,11 @@
+/* EHCI (USB 2.0) host controller driver: implements the usb_hcd_ops vtable
+   declared in usb_hcd.h.  Only high-speed devices are handled directly;
+   low/full-speed devices stay with the companion UHCI/OHCI controller. */
+#ifndef EHCI_H
+#define EHCI_H
+
+#include "usb_hcd.h"
+
+extern const struct usb_hcd_ops usb_ehci_ops;
+
+#endif

+ 20 - 0
kernel.c

@@ -362,6 +362,9 @@ void kernel_main(void)
 	bootcon_fb_info();
 	paging_init();
 	boot_trace("paging");
+	/* Keep a RAM copy of the boot console text; the WM hides the
+	 * framebuffer console later and this is what `bootlog` prints. */
+	bootcon_log_init();
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
 	 * before any drawing path can select an SSE2 helper. */
 	cpu_enable_sse();
@@ -501,6 +504,23 @@ void kernel_main(void)
 		dhcp_run(3000);
 	boot_trace("dhcp");
 
+	/* Dump the active NIC's counters after the boot DHCP attempt.  With
+	 * the ASIX as the management link this appears in netmon even when
+	 * the active e1000e cannot pass traffic. */
+	{
+		const struct netdev *nd = netdev_active();
+
+		if (nd && (nd->dump_brief || nd->dump_regs)) {
+			io_putstr("\n[boot netregs: ");
+			io_putstr(nd->name);
+			io_putstr("]\n");
+			if (nd->dump_brief)
+				nd->dump_brief();
+			else
+				nd->dump_regs();
+		}
+	}
+
 	/* Boot-time device/FS setup is complete; APs may now take user work.
 	 * Kernel threads stay pinned to the BSP and serialize on the BKL. */
 	smp_enable_sched();

+ 5 - 3
kernel.ld

@@ -29,6 +29,8 @@ SECTIONS
 	_end = .;
 }
 ASSERT(_end <= 0x7F000, "Kernel too large: would reach the run area at 0x80000");
-/* The legacy stage loads the compressed rootfs at 0x60000; the BSS clear and
- * the boot stack must stay below it. */
-ASSERT(_end <= 0x60000, "Kernel too large: would overlap the low rootfs pack");
+/* The legacy stage stages the compressed rootfs at 0x60000 before copying it
+ * to PACK_HIGH_ADDR; the loaded image (text/rodata/data) must not reach that
+ * staging area.  BSS and the boot stack may extend past it: the stage copy
+ * happens before the kernel clears BSS and starts using its stack. */
+ASSERT(__bss_start <= 0x60000, "Kernel image too large: legacy pack staging would overwrite it");

+ 11 - 11
mbr.S

@@ -142,25 +142,25 @@ part_table:
 	/* Partition 1: active, starts at LBA 63 (CHS 0/1/1, the classic DOS
 	 * start) and holds the boot sector (LBA 63) and kernel (LBA 64+).
 	 * The CHS values match the LBA values for the usual 255 heads /
-	 * 63 sectors geometry; BIOSes are known to validate that. */
+	 * 63 sectors geometry; BIOSes are known to validate that.
+	 * 1024 sectors leave room for a kernel of up to ~512 KB. */
 	.byte 0x80              /* bootable */
 	.byte 0x01, 0x01, 0x00  /* CHS start: C=0 H=1 S=1 (LBA 63) */
 	.byte 0x06              /* type: FAT16 */
-	.byte 0x08, 0x1C, 0x00  /* CHS end: C=0 H=8 S=28 (LBA 531) */
+	.byte 0x11, 0x10, 0x00  /* CHS end: C=0 H=17 S=16 (LBA 1086) */
 	.long 63                /* LBA start (boot sector) */
-	.long 469               /* boot sector + kernel sectors */
+	.long 1024              /* boot sector + kernel sectors */
 
 	/* Partition 2: dummy in the bare MBR.  usb.img replaces this entry
-	 * with the rootfs partition (same start, LBA 532, immediately behind
-	 * the kernel); keeping a second valid entry makes the stick look like
-	 * a USB hard disk to old AMI BIOSes (e.g. the ASUS AT3IONT-I), which
-	 * classify a single-partition MBR as a USB-ZIP disk and never offer
-	 * it.  Only partition 1 is active. */
+	 * with the rootfs partition at LBA 1088; keeping a second valid entry
+	 * makes the stick look like a USB hard disk to old AMI BIOSes (e.g.
+	 * the ASUS AT3IONT-I), which classify a single-partition MBR as a
+	 * USB-ZIP disk and never offer it.  Only partition 1 is active. */
 	.byte 0x00              /* not bootable */
-	.byte 0x08, 0x1d, 0x00  /* CHS start: C=0 H=8 S=29 (LBA 532) */
+	.byte 0x11, 0x12, 0x00  /* CHS start: C=0 H=17 S=18 (LBA 1088) */
 	.byte 0x21              /* type: dummy */
-	.byte 0x0d, 0x20, 0x01  /* CHS end: C=1 H=13 S=32 (LBA 16915) */
-	.long 532
+	.byte 0x16, 0x15, 0x01  /* CHS end: C=1 H=22 S=21 (LBA 17471) */
+	.long 1088
 	.long 16384
 	.space 32, 0            /* 2 empty entries */
 

+ 1 - 0
ne2000.c

@@ -402,4 +402,5 @@ void ne2000_print_info(void)
 const struct netdev ne2000_netdev = {
 	"ne2000", "RTL8029AS",
 	ne2000_init, ne2000_get_mac, ne2000_rx, ne2000_tx, ne2000_print_info, 0, 0, 0,
+	0, 0, 0, 0, 0,
 };

+ 55 - 21
net.c

@@ -242,12 +242,12 @@ void net_print_ip(void)
 
 static unsigned char udp_frame[14 + 20 + 8 + NET_UDP_MAX];
 
-int net_udp_send_from(const unsigned char *src_ip,
-		      const unsigned char *dst_mac, const unsigned char *dst_ip,
-		      unsigned int src_port, unsigned int dst_port,
-		      const void *payload, unsigned int len)
+int net_udp_send_from_dev(const struct netdev *nd,
+			  const unsigned char *src_ip,
+			  const unsigned char *dst_mac, const unsigned char *dst_ip,
+			  unsigned int src_port, unsigned int dst_port,
+			  const void *payload, unsigned int len)
 {
-	const struct netdev *nd = netdev_active();
 	unsigned char my_mac[6];
 	unsigned char *ip = udp_frame + 14;
 	unsigned char *udp = ip + 20;
@@ -295,6 +295,15 @@ int net_udp_send_from(const unsigned char *src_ip,
 	return nd->tx(udp_frame, 14 + total);
 }
 
+int net_udp_send_from(const unsigned char *src_ip,
+		      const unsigned char *dst_mac, const unsigned char *dst_ip,
+		      unsigned int src_port, unsigned int dst_port,
+		      const void *payload, unsigned int len)
+{
+	return net_udp_send_from_dev(netdev_active(), src_ip, dst_mac, dst_ip,
+				     src_port, dst_port, payload, len);
+}
+
 int net_udp_send(const unsigned char *dst_mac, const unsigned char *dst_ip,
 		 unsigned int src_port, unsigned int dst_port,
 		 const void *payload, unsigned int len)
@@ -306,16 +315,17 @@ int net_udp_send(const unsigned char *dst_mac, const unsigned char *dst_ip,
 				 payload, len);
 }
 
-int net_udp_parse(const unsigned char *f, unsigned int len,
-		  unsigned char src_ip[4], unsigned char dst_ip[4],
-		  unsigned int *src_port, unsigned int *dst_port,
-		  const unsigned char **payload, unsigned int *plen)
+static int net_udp_parse_common(const unsigned char *f, unsigned int len,
+				unsigned char src_ip[4],
+				unsigned char dst_ip[4],
+				unsigned int *src_port,
+				unsigned int *dst_port,
+				const unsigned char **payload,
+				unsigned int *plen, int check_dst)
 {
 	const unsigned char *ip = f + 14;
 	const unsigned char *udp;
-	unsigned char my_ip[4];
 	unsigned int i, ihl, ip_len;
-	int dst_ok = 0, dst_bcast = 1;
 
 	if (len < 14 + 20 + 8)
 		return 0;
@@ -331,17 +341,23 @@ int net_udp_parse(const unsigned char *f, unsigned int len,
 		return 0;
 
 	/* Accept datagrams for us or for the broadcast address (a DHCP client
-	 * has no address yet). */
-	net_get_ip(my_ip);
-	dst_ok = 1;
-	for (i = 0; i < 4; i++) {
-		if (ip[16 + i] != my_ip[i])
-			dst_ok = 0;
-		if (ip[16 + i] != 0xFF)
-			dst_bcast = 0;
+	 * has no address yet).  The DHCP client passes check_dst=0: a server
+	 * may send its OFFER unicast to the address it is offering, which is
+	 * not our (still static) IP. */
+	if (check_dst) {
+		unsigned char my_ip[4];
+		int dst_ok = 1, dst_bcast = 1;
+
+		net_get_ip(my_ip);
+		for (i = 0; i < 4; i++) {
+			if (ip[16 + i] != my_ip[i])
+				dst_ok = 0;
+			if (ip[16 + i] != 0xFF)
+				dst_bcast = 0;
+		}
+		if (!dst_ok && !dst_bcast)
+			return 0;
 	}
-	if (!dst_ok && !dst_bcast)
-		return 0;
 
 	udp = ip + ihl;
 	for (i = 0; i < 4; i++) {
@@ -355,6 +371,24 @@ int net_udp_parse(const unsigned char *f, unsigned int len,
 	return 1;
 }
 
+int net_udp_parse(const unsigned char *f, unsigned int len,
+		  unsigned char src_ip[4], unsigned char dst_ip[4],
+		  unsigned int *src_port, unsigned int *dst_port,
+		  const unsigned char **payload, unsigned int *plen)
+{
+	return net_udp_parse_common(f, len, src_ip, dst_ip, src_port,
+				    dst_port, payload, plen, 1);
+}
+
+int net_udp_parse_any(const unsigned char *f, unsigned int len,
+		      unsigned char src_ip[4], unsigned char dst_ip[4],
+		      unsigned int *src_port, unsigned int *dst_port,
+		      const unsigned char **payload, unsigned int *plen)
+{
+	return net_udp_parse_common(f, len, src_ip, dst_ip, src_port,
+				    dst_port, payload, plen, 0);
+}
+
 /* ---- ARP resolution (shared by the DNS resolver and the TCP client) ---- */
 
 /* ARP request "who has target? tell my_ip" -> 42-byte frame. */

+ 17 - 0
net.h

@@ -73,6 +73,14 @@ int net_udp_send_from(const unsigned char *src_ip,
 		      unsigned int src_port, unsigned int dst_port,
 		      const void *payload, unsigned int len);
 
+/* Same, but forced onto a specific NIC (netlog/netkexec management link). */
+struct netdev;
+int net_udp_send_from_dev(const struct netdev *nd,
+			  const unsigned char *src_ip,
+			  const unsigned char *dst_mac, const unsigned char *dst_ip,
+			  unsigned int src_port, unsigned int dst_port,
+			  const void *payload, unsigned int len);
+
 /* Parse one Ethernet frame as a UDP datagram addressed to us or to the
  * broadcast address.  Fills the addresses/ports and the payload location.
  * Returns 1 on match. */
@@ -81,6 +89,15 @@ int net_udp_parse(const unsigned char *frame, unsigned int len,
 		  unsigned int *src_port, unsigned int *dst_port,
 		  const unsigned char **payload, unsigned int *plen);
 
+/* Same, but without the destination-IP filter.  DHCP uses this because a
+ * server may send its OFFER unicast to the address it is offering, which is
+ * not our current (static) IP.  The protocol layer still validates the DHCP
+ * transaction ID and chaddr. */
+int net_udp_parse_any(const unsigned char *frame, unsigned int len,
+		      unsigned char src_ip[4], unsigned char dst_ip[4],
+		      unsigned int *src_port, unsigned int *dst_port,
+		      const unsigned char **payload, unsigned int *plen);
+
 /* Print one decoded frame summary line (dst/src/ethertype + ARP/IP/ICMP). */
 void net_decode_frame(const unsigned char *frame, unsigned int len);
 

+ 99 - 2
netdev.c

@@ -3,12 +3,14 @@
 #include "netdev.h"
 #include "asix.h"
 #include "e1000e.h"
+#include "io.h"
 #include "ne2000.h"
+#include "net.h"
 #include "process.h"
 #include "r8168.h"
 static const struct netdev *const netdev_drivers[] = {
-	/* Intel PCIe/PCH first: the X201's 82577LM is an e1000e.  On the
-	 * AT3IONT-I this probe misses and r8168 drives the real NIC. */
+	/* e1000e is the active test NIC; the ASIX is kept as the management
+	 * link by netdev_mgmt() even while e1000e is active. */
 	&e1000e_netdev,
 	&r8168_netdev,
 	&ne2000_netdev,
@@ -62,6 +64,15 @@ int netdev_in_use(void)
 	return busy != 0;
 }
 
+/* Apply the driver's link-local/static default when it becomes active.
+ * The ASIX direct link uses 192.168.50.2/24; the e1000e keeps the router
+ * subnet at 192.168.178.240. */
+static void netdev_apply_defaults(const struct netdev *nd)
+{
+	if (nd && nd->default_ip)
+		net_configure(nd->default_ip, nd->default_mask, 0);
+}
+
 const struct netdev *netdev_active(void)
 {
 	unsigned int i;
@@ -71,6 +82,7 @@ const struct netdev *netdev_active(void)
 		for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
 			if (netdev_drivers[i]->init() == 0) {
 				active = netdev_drivers[i];
+				netdev_apply_defaults(active);
 				break;
 			}
 		}
@@ -78,6 +90,16 @@ const struct netdev *netdev_active(void)
 	return active;
 }
 
+/* The ASIX dongle is the direct management link when present.  Keeping
+ * netlog/netkexec here lets the active driver be switched to e1000e for
+ * testing without losing the netmon console or `make net-dev`. */
+const struct netdev *netdev_mgmt(void)
+{
+	if (asix_netdev.init() == 0)
+		return &asix_netdev;
+	return netdev_active();
+}
+
 int netdev_get_mac(unsigned char mac[6])
 {
 	const struct netdev *nd = netdev_active();
@@ -91,3 +113,78 @@ int netdev_get_mac(unsigned char mac[6])
 	nd->get_mac(mac);
 	return 0;
 }
+
+static int name_eq(const char *a, const char *b)
+{
+	while (*a && *b) {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+	}
+	return *a == 0 && *b == 0;
+}
+
+int netdev_select(const char *name)
+{
+	unsigned int i;
+
+	if (!name || !*name || busy)
+		return -1;
+	while (*name == ' ')
+		name++;
+	for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
+	     i++) {
+		if (!name_eq(netdev_drivers[i]->name, name))
+			continue;
+		if (active == netdev_drivers[i]) {
+			probed = 1;
+			return 0;
+		}
+		if (netdev_drivers[i]->init() != 0)
+			return -1;
+		active = netdev_drivers[i];
+		probed = 1;
+		netdev_apply_defaults(active);
+		return 0;
+	}
+	return -1;
+}
+
+static void print_netdev(const char *label, const struct netdev *nd)
+{
+	io_putstr("netdev: ");
+	io_putstr(label);
+	io_putstr(nd->name);
+	io_putstr(" (");
+	io_putstr(nd->chip);
+	io_putstr(")\n");
+}
+
+void program_netdev_main(const char *arg)
+{
+	const struct netdev *mgmt;
+
+	if (arg && *arg) {
+		while (*arg == ' ')
+			arg++;
+		if (netdev_select(arg) != 0) {
+			io_putstr("netdev: cannot select '");
+			io_putstr(arg);
+			io_putstr("'\n");
+			return;
+		}
+	} else if (!netdev_active()) {
+		io_putstr("netdev: no NIC\n");
+		return;
+	}
+
+	print_netdev("active ", netdev_active());
+	if (netdev_active()->print_info)
+		netdev_active()->print_info();
+	mgmt = netdev_mgmt();
+	if (mgmt && mgmt != netdev_active())
+		print_netdev("mgmt   ", mgmt);
+	if (!(arg && *arg))
+		io_putstr("netdev: use netdev asix|e1000e|r8168|ne2000\n");
+}

+ 20 - 0
netdev.h

@@ -17,12 +17,32 @@ struct netdev {
 	void (*dump_regs)(void);    /* register dump; may be NULL */
 	int  (*set_loopback)(int on);   /* PHY loopback for self-test; may be NULL */
 	void (*reset)(void);        /* full NIC re-init (recover from a stall); may be NULL */
+	int  (*set_speed)(unsigned int mbps); /* 0 = auto, 100 = force 100M; may be NULL */
+	void (*set_mac)(const unsigned char mac[6]); /* runtime MAC override; may be NULL */
+	/* Address applied when this driver becomes active.  The direct ASIX
+	 * link uses its own subnet so it can coexist with the e1000e link to
+	 * the router.  NULL leaves the current address alone. */
+	const unsigned char *default_ip;
+	const unsigned char *default_mask;
+	/* Short RX summary for the boot log (a full register dump can
+	 * overflow the netlog ring before the WM starts); may be NULL. */
+	void (*dump_brief)(void);
 };
 
 /* First registered driver that probes successfully (probe result is cached);
  * NULL when no NIC is present. */
 const struct netdev *netdev_active(void);
 
+/* Management NIC for netlog/netkexec.  Prefers the ASIX dongle so the
+ * direct debug link survives `netdev e1000e`; falls back to the active
+ * driver. */
+const struct netdev *netdev_mgmt(void);
+
+/* Select a NIC by short driver name ("e1000e", "r8168", "ne2000", "asix").
+ * Returns 0 on success; leaves the current driver active on failure. */
+int netdev_select(const char *name);
+void program_netdev_main(const char *arg);
+
 /* Busy gate for commands that poll the NIC for a while (netrecv, ping,
  * netloop, netkexec): the background kexec watcher must not steal
  * frames while one of them owns the receiver. */

+ 86 - 44
netkexec.c

@@ -24,6 +24,7 @@
 #include "kexec.h"
 #include "net.h"
 #include "netdev.h"
+#include "process.h"
 #include "serial.h"
 #include "timer.h"
 #include "netkexec.h"
@@ -89,17 +90,10 @@ static void wr32(unsigned char *p, unsigned int v)
 	p[3] = (unsigned char)((v >> 24) & 0xFF);
 }
 
-static int ip_eq(const unsigned char *a, const unsigned char *b)
-{
-	return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
-}
-
-static int ip_is_bcast(const unsigned char *ip)
-{
-	return ip[0] == 0xFF && ip[1] == 0xFF && ip[2] == 0xFF && ip[3] == 0xFF;
-}
-
 /* Parse `f` as a UDP datagram to NETKEXEC_PORT; fill payload + peer address.
+ * The management link may have a different address from the active test NIC
+ * (ASIX 192.168.50.2 while e1000e is 192.168.178.240), so accept any
+ * destination IP here: the KXN2/KXND magic and checksum are the gate.
  * Returns 1 on match. */
 static int parse_udp(const unsigned char *f, unsigned int len,
 		     const unsigned char **payload, unsigned int *plen,
@@ -108,7 +102,6 @@ static int parse_udp(const unsigned char *f, unsigned int len,
 {
 	const unsigned char *ip = f + 14;
 	const unsigned char *udp;
-	unsigned char my_ip[4];
 	unsigned int i, ihl, ip_len;
 
 	if (len < 14 + 20 + 8)
@@ -124,10 +117,6 @@ static int parse_udp(const unsigned char *f, unsigned int len,
 	if (ip_len < ihl + 8 || 14 + ip_len > len)
 		return 0;
 
-	net_get_ip(my_ip);
-	if (!ip_eq(ip + 16, my_ip) && !ip_is_bcast(ip + 16))
-		return 0;
-
 	udp = ip + ihl;
 	if ((((unsigned int)udp[2] << 8) | udp[3]) != NETKEXEC_PORT)
 		return 0;
@@ -142,38 +131,56 @@ static int parse_udp(const unsigned char *f, unsigned int len,
 	return *plen > 0;
 }
 
+static int netkexec_poll_nd(const struct netdev *nd, int capture)
+{
+	unsigned int n;
+
+	for (n = 0; n < 8; n++) {
+		int len = nd->rx(rxbuf, sizeof(rxbuf));
+
+		if (len < 0)
+			break;
+		if (capture && netkexec_capture_frame(rxbuf,
+						      (unsigned int)len))
+			return 1;
+
+		/* Not a kexec datagram: stay reachable by answering ARP and
+		 * ICMP echo requests for our IP (so the board can be pinged,
+		 * and a host can resolve its MAC, without running a
+		 * command).  Until an address is configured there is nothing
+		 * to answer for. */
+		if (net_is_configured()) {
+			int rlen = net_build_reply(rxbuf, (unsigned int)len,
+						   replybuf,
+						   sizeof(replybuf));
+			if (rlen > 0)
+				nd->tx(replybuf, (unsigned int)rlen);
+		}
+	}
+	return 0;
+}
+
 int netkexec_pending(void)
 {
-	const struct netdev *nd;
-	int len;
+	const struct netdev *mgmt, *act;
 
 	if (kexec_busy || netdev_in_use())
 		return 0;
 	if (have_header)
 		return 1;
-	nd = netdev_active();
-	if (!nd)
+	mgmt = netdev_mgmt();
+	act = netdev_active();
+	if (!mgmt && !act)
 		return 0;
 
-	/* One frame per call; anything else is dropped (this also keeps the RX
-	 * ring drained while the system idles at a prompt). */
-	len = nd->rx(rxbuf, sizeof(rxbuf));
-	if (len < 0)
-		return 0;
-
-	if (netkexec_capture_frame(rxbuf, (unsigned int)len))
+	/* KXN2/KXNP headers and the netmon console use the management
+	 * link (ASIX when present), so switching the active driver to
+	 * e1000e for testing does not cut the upload/debug path.  The
+	 * active NIC still gets its ARP/ICMP responder polled. */
+	if (mgmt && netkexec_poll_nd(mgmt, 1))
 		return 1;
-
-	/* Not a kexec datagram: stay reachable by answering ARP and ICMP echo
-	 * requests for our IP (so the board can be pinged, and a host can
-	 * resolve its MAC, without running a command).  Until an address is
-	 * configured there is nothing to answer for. */
-	if (net_is_configured()) {
-		int rlen = net_build_reply(rxbuf, (unsigned int)len,
-					   replybuf, sizeof(replybuf));
-		if (rlen > 0)
-			nd->tx(replybuf, (unsigned int)rlen);
-	}
+	if (act && act != mgmt)
+		netkexec_poll_nd(act, 0);
 	return 0;
 }
 
@@ -230,19 +237,36 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 
 static void send_ack(unsigned int next_offset, unsigned int status)
 {
+	const struct netdev *nd = netdev_mgmt();
+	unsigned char src[4];
+
+	if (!nd)
+		return;
+	if (nd->default_ip) {
+		unsigned int i;
+
+		for (i = 0; i < 4; i++)
+			src[i] = nd->default_ip[i];
+	} else {
+		net_get_ip(src);
+	}
 	wr32(ackbuf, X_MAGIC_ACK);
 	wr32(ackbuf + 4, next_offset);
 	wr32(ackbuf + 8, status);
-	net_udp_send(peer_mac, peer_ip, NETKEXEC_PORT, peer_port,
-		     ackbuf, sizeof(ackbuf));
+	net_udp_send_from_dev(nd, src, peer_mac, peer_ip, NETKEXEC_PORT,
+			      peer_port, ackbuf, sizeof(ackbuf));
 }
 
 static int netkexec_recv(unsigned int stage_base)
 {
 	volatile unsigned char *stage = (volatile unsigned char *)stage_base;
+	const struct netdev *nd = netdev_mgmt();
 	unsigned int offset = 0, retries = 0, sum = 0;
 	unsigned int deadline = get_ticks_ms() + 3000;
+	unsigned int hard_deadline = get_ticks_ms() + 60000;
 
+	if (!nd)
+		return -1;
 	kexec_total = x_size;
 	kexec_progress = 0;
 	send_ack(0, ACK_CONTINUE);          /* header ok, send from offset 0 */
@@ -252,6 +276,13 @@ static int netkexec_recv(unsigned int stage_base)
 		unsigned int plen, port, doff, dlen;
 		int len, i;
 
+		/* A host that trickles one datagram at a time can keep the
+		 * per-packet deadline alive forever, holding the NIC (and the
+		 * background ARP/ICMP responder) hostage.  Cap the whole
+		 * transfer as well. */
+		if ((int)(get_ticks_ms() - hard_deadline) > 0)
+			return -1;
+
 		if ((int)(get_ticks_ms() - deadline) > 0) {
 			if (++retries > 10)
 				return -1;      /* host gave up */
@@ -260,19 +291,30 @@ static int netkexec_recv(unsigned int stage_base)
 			continue;
 		}
 
-		len = netdev_active()->rx(rxbuf, sizeof(rxbuf));
-		if (len < 0)
+		len = nd->rx(rxbuf, sizeof(rxbuf));
+		if (len < 0) {
+			/* Yield so the WM and terminal keep running while we
+			 * wait for the next datagram; this used to busy-spin
+			 * with the BKL held and froze the UI. */
+			process_sleep_ms(1);
 			continue;
+		}
 		if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
-			       peer_mac, peer_ip, &port))
+			       peer_mac, peer_ip, &port)) {
+			process_sleep_ms(1);
 			continue;
-		if (plen < 12 || rd32(payload) != X_MAGIC_DATA)
+		}
+		if (plen < 12 || rd32(payload) != X_MAGIC_DATA) {
+			process_sleep_ms(1);
 			continue;
+		}
 
 		doff = rd32(payload + 4);
 		dlen = rd32(payload + 8);
-		if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
+		if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen) {
+			process_sleep_ms(1);
 			continue;
+		}
 
 		if (doff == offset) {
 			for (i = 0; i < (int)dlen; i++) {

+ 15 - 3
netlog.c

@@ -80,10 +80,22 @@ static void send_line(const unsigned char *buf, unsigned int len)
 	static const unsigned char bcast_mac[6] = {
 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
 	};
-	unsigned char dst[4];
+	const struct netdev *nd = netdev_mgmt();
+	unsigned char src[4], dst[4];
 
+	if (!nd)
+		return;
 	netlog_get_host(dst);
-	net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, buf, len);
+	if (nd->default_ip) {
+		unsigned int i;
+
+		for (i = 0; i < 4; i++)
+			src[i] = nd->default_ip[i];
+	} else {
+		net_get_ip(src);
+	}
+	net_udp_send_from_dev(nd, src, bcast_mac, dst,
+			      NETLOG_PORT, NETLOG_PORT, buf, len);
 }
 
 void netlog_flush(void)
@@ -93,7 +105,7 @@ void netlog_flush(void)
 	static const char dropmsg[] = "[netlog: output dropped, buffer full]\n";
 	unsigned int i;
 
-	if (!enabled || netdev_in_use() || !netdev_active())
+	if (!enabled || netdev_in_use() || !netdev_mgmt())
 		return;
 
 	avail = pending();

+ 81 - 4
ngui.c

@@ -484,8 +484,6 @@ unsigned int ngui_text_width(const char *s)
 
 unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
 {
-	unsigned int i = 0;
-
 	(void)s;
 	if (scale < 1)
 		scale = 1;
@@ -519,6 +517,37 @@ void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
 			   color, scale, bold);
 }
 
+/* Draw a string upside down.  The whole n-character block is rotated, so the
+ * first character ends up on the right; used for the mirrored corner index on
+ * a playing card. */
+void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
+		      unsigned int n, unsigned short color)
+{
+	unsigned int i;
+	int row, col;
+
+	for (i = 0; i < n && s && s[i]; i++) {
+		const unsigned char *g =
+			&font_8x16[(unsigned int)(unsigned char)s[i] *
+				   FONT_8X16_ROWS];
+
+		for (row = 0; row < FONT_8X16_ROWS; row++) {
+			unsigned char bits = g[row];
+
+			if (!bits)
+				continue;
+			for (col = 0; col < 8; col++) {
+				if (!(bits & (0x80u >> col)))
+					continue;
+				ngui_pixel(ui,
+					   x + (int)(n - 1 - i) * 8 + 7 - col,
+					   y + FONT_8X16_ROWS - 1 - row,
+					   color);
+			}
+		}
+	}
+}
+
 void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
 			const char *s, unsigned short color)
 {
@@ -641,14 +670,62 @@ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	return fired;
 }
 
-int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
-		     enum ngui_icon icon)
+/* One row of a drop-down menu: left-aligned label, optional check mark and
+ * right-aligned shortcut.  Uses the same press/release/active semantics as
+ * ngui_button so a menu and its caller never disagree about a click. */
+int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		   const char *label, const char *shortcut, int checked)
 {
 	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
 	int press_hot = ui->pressed &&
 		ngui_rect_contains(&r, ui->press_x, ui->press_y);
 	int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
 	int fired = 0;
+	int selected;
+	unsigned short bg, fg;
+	int ty;
+
+	if (press_hot)
+		ui->active = id;
+	if (ui->pressed && ui->released && press_hot && release_hot)
+		fired = 1;
+	if (ui->released && ui->active == id) {
+		if (release_hot)
+			fired = 1;
+		ui->active = 0;
+	}
+
+	selected = (ui->active == id) || hot;
+	bg = selected ? ui->theme.selection : ui->theme.panel;
+	fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
+	ngui_fill_rect(ui, r, bg);
+
+	ty = r.y + (r.h - 16) / 2;
+	ngui_push_clip(ui, r);
+	if (checked) {
+		ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
+		ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
+	}
+	if (label)
+		ngui_text(ui, r.x + 22, ty, label, fg);
+	if (shortcut) {
+		int sw = (int)ngui_text_width(shortcut);
+
+		ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
+	}
+	ngui_pop_clip(ui);
+	if (hot)
+		ui->hot = id;
+	return fired;
+}
+
+int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		     enum ngui_icon icon){
+	int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
+	int press_hot = ui->pressed &&
+		ngui_rect_contains(&r, ui->press_x, ui->press_y);
+	int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
+	int fired = 0;
 	unsigned short bg;
 
 	if (press_hot)

+ 4 - 0
ngui.h

@@ -155,6 +155,8 @@ void ngui_text_n(struct ngui *ui, int x, int y, const char *s,
 		 unsigned int n, unsigned short color);
 void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
 		      unsigned int n, unsigned short color, int scale, int bold);
+void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
+		      unsigned int n, unsigned short color);
 void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
 			const char *s, unsigned short color);
 unsigned int ngui_text_width(const char *s);
@@ -165,6 +167,8 @@ void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
 /* Widgets.  IDs must be stable across frames. */
 int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		const char *label);
+int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
+		   const char *label, const char *shortcut, int checked);
 int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		     enum ngui_icon icon);
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,

+ 1 - 0
ohci.c

@@ -683,4 +683,5 @@ const struct usb_hcd_ops usb_ohci_ops = {
 	ohci_int_in_stop,
 	0,
 	0,
+	0,
 };

+ 1 - 0
r8168.c

@@ -985,4 +985,5 @@ const struct netdev r8168_netdev = {
 	"r8168", "RTL8111/8168",
 	r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
 	r8168_dump_regs, r8168_set_loopback, r8168_reset,
+	0, 0, 0, 0, 0,
 };

+ 2 - 1
sys_info.c

@@ -75,7 +75,8 @@ int sys_mouseinfo(struct sys_mouseinfo *out)
 		out->usb_ep = ep;
 		out->usb_mps = mps;
 		out->usb_low = (unsigned int)low;
-		copy_str(out->hcd, usb_hcd_name(), sizeof(out->hcd));
+		copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
+			 sizeof(out->hcd));
 	}
 	out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
 	return 1;

+ 42 - 0
sys_net.c

@@ -98,7 +98,11 @@ int sys_net_print_info(void)
 
 	if (!nd || !nd->print_info)
 		return -1;
+	/* print_info reads the PHY via MDIC and can wait on the PHY/ME
+	 * semaphore; the syscall stub entered with interrupts off. */
+	net_enter();
 	nd->print_info();
+	net_exit();
 	return 0;
 }
 
@@ -188,7 +192,9 @@ int sys_net_regs(void)
 
 	if (!nd || !nd->dump_regs)
 		return -1;
+	net_enter();
 	nd->dump_regs();
+	net_exit();
 	return 0;
 }
 
@@ -201,11 +207,47 @@ int sys_net_reset(void)
 	/* Own the receiver for the whole reset, so the netlog kworker cannot
 	 * submit frames while the MAC and rings are being reinitialised. */
 	netdev_acquire();
+	/* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
+	 * entered with interrupts off. */
+	net_enter();
 	nd->reset();
+	net_exit();
 	netdev_release();
 	return 0;
 }
 
+/* net100 fallback: force 100M or restore auto.  The driver drops and
+ * renegotiates the link, which can take a few seconds; own the receiver so
+ * the background watcher cannot poll frames while the link is down. */
+int sys_net_speed(unsigned int mbps)
+{
+	const struct netdev *nd = netdev_active();
+	int r;
+
+	if (!nd || !nd->set_speed)
+		return -1;
+	if (sys_net_acquire() != 0)
+		return -2;
+	net_enter();
+	r = nd->set_speed(mbps);
+	net_exit();
+	sys_net_release();
+	return r;
+}
+
+int sys_net_mac_set(const unsigned char mac[6])
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->set_mac)
+		return -1;
+	if (sys_net_acquire() != 0)
+		return -2;
+	nd->set_mac(mac);
+	sys_net_release();
+	return 0;
+}
+
 int sys_dhcp_run(unsigned int timeout_ms)
 {
 	int r;

+ 2 - 0
sys_net.h

@@ -26,6 +26,8 @@ int sys_net_reply(const unsigned char *in, unsigned int len,
 int sys_net_loopback(int on);
 int sys_net_regs(void);
 int sys_net_reset(void);
+int sys_net_speed(unsigned int mbps);
+int sys_net_mac_set(const unsigned char mac[6]);
 int sys_dhcp_run(unsigned int timeout_ms);
 int sys_dhcp_info(unsigned char server[4], unsigned int *lease);
 int sys_dns_resolve(const char *name, unsigned char ip[4]);

+ 4 - 0
syscall.c

@@ -117,6 +117,10 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_net_regs();
 	case SYS_NET_RESET:
 		return sys_net_reset();
+	case SYS_NET_SPEED:
+		return sys_net_speed((unsigned int)a1);
+	case SYS_NET_MAC_SET:
+		return sys_net_mac_set((const unsigned char *)a1);
 	case SYS_DHCP_RUN:
 		return sys_dhcp_run((unsigned int)a1);
 	case SYS_DHCP_INFO:

+ 2 - 0
syscall.h

@@ -132,6 +132,8 @@
 #define SYS_TCP_SRV_SEND   96  /* (buf, len) -> 0/-1/-2/-3 */
 #define SYS_TCP_SRV_CLOSE  97
 #define SYS_TCP_PEER       99  /* (ip[4], unsigned *port) -> 0/-1 (debug log) */
+#define SYS_NET_SPEED     100  /* (mbps) -> 0; 0 = auto, 100 = force 100M */
+#define SYS_NET_MAC_SET   101  /* (const mac[6]) -> 0/-1/-2 */
 
 /* Stat one path (the file service has no open handle). */
 #define SYS_FS_STAT        98  /* (path, struct sys_stat *out) -> 0/<0 */

+ 52 - 1
terminal.c

@@ -2,12 +2,14 @@
    Commands are spawned as separate processes for preemptive multitasking. */
 
 #include "io.h"
+#include "bootcon.h"
 #include "dbg.h"
 #include "idle.h"
 #include "kexec.h"
 #include "kmalloc.h"
 #include "mm.h"
 #include "process.h"
+#include "sys_fs.h"
 #include "terminal.h"
 #include "timer.h"
 
@@ -21,6 +23,7 @@ extern void program_serial_recv_main(const char *arg);
 extern void program_run_serial_main(const char *arg);
 extern void program_run_embedded_main(const char *name);
 extern void program_lsusb_main(void);
+extern void program_netdev_main(const char *arg);
 extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
@@ -85,6 +88,7 @@ static int term_path_spawn(struct term *t, const char *word, const char *args);
 
 static int cmd_cd(struct term *t, const char *args);
 static int cmd_clear(struct term *t, const char *args);
+static int cmd_bootlog(struct term *t, const char *args);
 static int cmd_dbg(struct term *t, const char *args);
 static int cmd_format(struct term *t, const char *args);
 static int cmd_help(struct term *t, const char *args);
@@ -107,6 +111,7 @@ static const struct kcmd cmds[] = {
 	{ "path",       "path [dir[:dir...]]",           CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_path },
 	{ "help",       "help",                          CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
 	{ "clear",      "clear",                         CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_clear },
+	{ "bootlog",    "bootlog",                       CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_bootlog },
 	{ "run",        "run <name>|<file>",             CK_CUSTOM, 0, 0, 0, 0, cmd_run },
 	{ "space",      "space",                         CK_SPAWN, 0, 0, program_run_embedded_main, "space", 0 },
 	{ "prog",       "prog",                          CK_SPAWN, 0, 0, program_run_embedded_main, "prog", 0 },
@@ -115,6 +120,7 @@ static const struct kcmd cmds[] = {
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_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 },
+	{ "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 },
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
 	{ "peek",       "peek <addr> [len]",             CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
@@ -259,6 +265,50 @@ static int cmd_clear(struct term *t, const char *args)
 	return 1;   /* prompt already printed */
 }
 
+/* Dump the whole boot console text captured by bootcon_puts().  The ring-3
+ * terminal keeps scrollback, so no paging is needed; sleep briefly every
+ * 512 bytes so the terminal can drain its tty ring while we write. */
+static int cmd_bootlog(struct term *t, const char *args)
+{
+	const char *p;
+	unsigned int len, i;
+
+	(void)t;
+	p = bootcon_log(&len);
+	while (*args == ' ')
+		args++;
+	if (args[0] == 's' && args[1] == 'a' && args[2] == 'v' &&
+	    args[3] == 'e' && (args[4] == ' ' || args[4] == '\0')) {
+		const char *path = args + 4;
+		int r;
+
+		while (*path == ' ')
+			path++;
+		if (!*path) {
+			io_putstr("bootlog: usage bootlog save <path>\n");
+			return 0;
+		}
+		r = sys_write_file(path, p, len);
+		if (r == 0)
+			io_putstr("bootlog saved\n");
+		else {
+			io_putstr("bootlog: save failed (");
+			io_puthex((unsigned int)r);
+			io_putstr(")\n");
+		}
+		return 0;
+	}
+
+	for (i = 0; i < len; i++) {
+		io_putchar(p[i]);
+		if ((i & 511u) == 511u)
+			process_sleep_ms(1);
+	}
+	if (bootcon_log_truncated())
+		io_putstr("\n[boot log truncated]\n");
+	return 0;
+}
+
 static int cmd_dbg(struct term *t, const char *args)
 {
 	(void)t;
@@ -1474,7 +1524,8 @@ int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
 	unsigned int i;
 	int pid;
 
-	if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help))
+	if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help &&
+		   c->custom != cmd_bootlog))
 		return -1;
 	ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
 	if (!ctx)

+ 7 - 1
tools/qmp_cursor_test.py

@@ -9,6 +9,8 @@ Modes:
   --uhci      UHCI (piix3) controller
   --uhci72    UHCI at PCI 00:01.2 (multifunction south-bridge layout)
   --multi     three OHCI controllers, mouse on the third
+  --xhci      xHCI controller, mouse on it
+  --ehci      EHCI + UHCI companion, mouse on the companion (multi-HCD)
   --hotplug   boot without the mouse, plug it in, move, unplug, replug
 
 Requires: floppy.img/root.img built (make all), python3, QEMU.
@@ -118,7 +120,7 @@ def move(s, dx, dy):
 
 def main():
     mode = "ohci"
-    for m in ("uhci", "uhci72", "multi", "hotplug", "xhci"):
+    for m in ("uhci", "uhci72", "multi", "hotplug", "xhci", "ehci"):
         if "--" + m in sys.argv:
             mode = m
 
@@ -143,6 +145,10 @@ def main():
         qemu += ["-device", "pci-ohci"]
     elif mode == "xhci":
         qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
+    elif mode == "ehci":
+        qemu += ["-device", "usb-ehci,id=ehci0",
+                 "-device", "piix3-usb-uhci,id=uhci0",
+                 "-device", "usb-mouse,bus=uhci0.0"]
     else:
         qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
     qemu += ["-drive", "file=floppy.img,format=raw,if=floppy",

+ 11 - 2
tools/qmp_keyboard_test.py

@@ -7,6 +7,8 @@ verifies the screen content changed (the command output).
 Modes:
   (default)   OHCI + usb-mouse + usb-kbd; types "help"
   --hotplug   boots without the keyboard, plugs it in via QMP, then types
+  --xhci      xHCI controller, keyboard on it
+  --ehci      EHCI + UHCI companion, keyboard on the companion (multi-HCD)
   --keys a,b,ret   type a custom key list (QEMU qcodes)
 
 Requires: floppy.img/root.img built (make all), python3, QEMU.
@@ -61,6 +63,7 @@ def read_ppm(path):
 def main():
     hotplug = "--hotplug" in sys.argv
     xhci = "--xhci" in sys.argv
+    ehci = "--ehci" in sys.argv
     keys = ["h", "e", "l", "p", "ret"]
     if "--keys" in sys.argv:
         keys = sys.argv[sys.argv.index("--keys") + 1].split(",")
@@ -75,9 +78,15 @@ def main():
     ppm = os.path.join(workdir, "screen.ppm")
 
     kbddev = [] if hotplug else ["-device", "usb-kbd"]
+    if ehci and not hotplug:
+        kbddev = ["-device", "usb-kbd,bus=uhci0.0"]
     qemu = ["qemu-system-i386", "-M", "pc"]
     if xhci:
         qemu += ["-device", "qemu-xhci", "-device", "usb-mouse"]
+    elif ehci:
+        qemu += ["-device", "usb-ehci,id=ehci0",
+                 "-device", "piix3-usb-uhci,id=uhci0",
+                 "-device", "usb-mouse,bus=uhci0.0"]
     else:
         qemu += ["-device", "pci-ohci", "-device", "usb-mouse"]
     qemu += kbddev + [
@@ -98,7 +107,7 @@ def main():
         s.connect(sock)
         qmp_recv(s)                  # greeting
         qmp_cmd(s, "qmp_capabilities")
-        time.sleep(4)                # boot
+        time.sleep(5)                # boot
 
         if hotplug:
             qmp_cmd(s, "device_add", {"driver": "usb-kbd", "id": "kbd"})
@@ -113,7 +122,7 @@ def main():
         for k in keys:
             qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": k}]})
             time.sleep(0.25)
-        time.sleep(1.0)
+        time.sleep(4.0)
 
         if os.path.exists(ppm):
             os.unlink(ppm)

+ 69 - 0
ucmd_net100.c

@@ -0,0 +1,69 @@
+/* net100: ring-3 command.  Force 100BASE-TX or restore auto/1G.
+ *
+ * This is the diagnostic fallback for a marginal 1000M pair/jack: 100BASE-TX
+ * uses only two pairs, so a link that reports 1000M but loses DHCP/ARP
+ * broadcasts can still be fully usable.  The driver keeps the setting until
+ * `net100 off` or reboot. */
+
+#include "ucmd_lib.h"
+
+static int arg_is(const char *arg, const char *word)
+{
+	unsigned int i = 0;
+
+	while (arg[i] && word[i] && arg[i] == word[i])
+		i++;
+	return arg[i] == '\0' && word[i] == '\0';
+}
+
+static void usage(void)
+{
+	u_puts("net100: usage net100 [on|off]  (on = force 100M, off = auto/1G)\n");
+}
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+	unsigned int mbps;
+	int r;
+
+	if (!arg || !arg[0] || arg_is(arg, "on") || arg_is(arg, "100"))
+		mbps = 100;
+	else if (arg_is(arg, "off") || arg_is(arg, "auto") ||
+		 arg_is(arg, "1000"))
+		mbps = 0;
+	else {
+		usage();
+		u_exit(1);
+	}
+
+	if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
+		u_puts("net100: no NIC found\n");
+		u_exit(1);
+	}
+
+	r = u_syscall1(SYS_NET_SPEED, (int)mbps);
+	if (r == -1) {
+		u_puts("net100: speed control not supported by ");
+		u_puts(n.name);
+		u_putc('\n');
+		u_exit(1);
+	}
+	if (r == -2) {
+		u_puts("net100: NIC is busy (see ps)\n");
+		u_exit(1);
+	}
+
+	u_puts(mbps == 100 ? "net100: forcing 100M\n"
+			   : "net100: restoring auto/1G\n");
+	u_syscall0(SYS_NET_PRINT_INFO);
+	if (r == -3) {
+		u_puts("net100: link did not come up; check the cable/port\n");
+		u_exit(1);
+	}
+	if (r == -4) {
+		u_puts("net100: link is up but not at 100M\n");
+		u_exit(1);
+	}
+	u_exit(0);
+}

+ 72 - 0
ucmd_netmac.c

@@ -0,0 +1,72 @@
+/* netmac: ring-3 diagnostic.  Temporarily override the NIC's MAC address.
+ *
+ * Used to test whether a switch/repeater is bound to a particular MAC: set
+ * the board to the ION's MAC (with the ION powered off), then run dhcp/ping.
+ * The setting is RAM-only and disappears on reboot. */
+
+#include "ucmd_lib.h"
+
+static int hexval(char c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
+	if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+	return -1;
+}
+
+static int parse_mac(const char *s, unsigned char mac[6])
+{
+	unsigned int i;
+
+	for (i = 0; i < 6; i++) {
+		int hi = hexval(s[0]);
+		int lo = hexval(s[1]);
+
+		if (hi < 0 || lo < 0)
+			return -1;
+		mac[i] = (unsigned char)((hi << 4) | lo);
+		s += 2;
+		if (i != 5) {
+			if (*s != ':' && *s != '-')
+				return -1;
+			s++;
+		}
+	}
+	return *s == '\0' ? 0 : -1;
+}
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+	unsigned char old[6], mac[6];
+
+	if (!arg || !arg[0]) {
+		u_puts("netmac: usage netmac <xx:xx:xx:xx:xx:xx>\n");
+		u_exit(1);
+	}
+	if (parse_mac(arg, mac) != 0) {
+		u_puts("netmac: invalid MAC address\n");
+		u_exit(1);
+	}
+	if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
+		u_puts("netmac: no NIC found\n");
+		u_exit(1);
+	}
+
+	u_syscall1(SYS_NET_MAC, (int)old);
+	if (u_syscall1(SYS_NET_MAC_SET, (int)mac) != 0) {
+		u_puts("netmac: NIC busy or MAC override unsupported\n");
+		u_exit(1);
+	}
+
+	u_puts("netmac: ");
+	u_put_mac(old);
+	u_puts(" -> ");
+	u_put_mac(mac);
+	u_puts("\nnetmac: RAM-only; reboot restores the factory MAC\n");
+	u_syscall0(SYS_NET_PRINT_INFO);
+	u_exit(0);
+}

+ 2682 - 0
ucmd_solitaire.c

@@ -0,0 +1,2682 @@
+/* solitaire: a Windows 95 style Klondike solitaire for a canvas window.
+ *
+ * Seven tableau piles, four foundations, a stock and a waste.  Draw one or
+ * three, drag cards or whole runs, double-click to send a card to a
+ * foundation, and click the stock to deal / recycle.  The look follows the
+ * old Windows game: green felt, a grey Game/Help menu, white cards, a blue
+ * patterned back and the bouncing cards after a win.
+ *
+ * Mouse only (the kernel keyboard path has no F2), so N starts a new game.
+ * The window can be resized; the table is re-laid out.
+ */
+
+#include "ucmd_lib.h"
+#include "ngui.h"
+
+#define TABLEAU_PILES   7
+#define FOUNDATION_PILES 4
+#define RANK_COUNT      13
+#define DECK_SIZE       52
+
+#define MENU_H          22
+#define STATUS_H        20
+
+#define START_W         760
+#define START_H         540
+
+#define C_FELT          NGUI_RGB(0, 128, 0)
+#define C_SLOT_FILL     NGUI_RGB(0, 112, 0)
+#define C_SLOT_LINE     NGUI_RGB(0, 84, 0)
+#define C_SLOT_ICON     NGUI_RGB(96, 190, 96)
+#define C_MENU          NGUI_RGB(192, 192, 192)
+#define C_MENU_TOP      NGUI_RGB(255, 255, 255)
+#define C_MENU_DARK     NGUI_RGB(64, 64, 64)
+#define C_MENU_HOT      NGUI_RGB(206, 206, 206)
+#define C_TEXT          NGUI_RGB(0, 0, 0)
+#define C_TEXT_LIGHT    NGUI_RGB(255, 255, 255)
+#define C_NAVY          NGUI_RGB(0, 0, 128)
+#define C_CARD          NGUI_RGB(255, 255, 255)
+#define C_CARD_EDGE     NGUI_RGB(40, 40, 40)
+#define C_SHADOW        NGUI_RGB(0, 82, 0)
+#define C_RED           NGUI_RGB(200, 0, 0)
+#define C_BLACK         NGUI_RGB(0, 0, 0)
+#define C_GOLD          NGUI_RGB(184, 144, 64)
+#define C_CREAM         NGUI_RGB(255, 248, 220)
+#define C_SKIN          NGUI_RGB(255, 222, 189)
+#define C_HAIR          NGUI_RGB(72, 48, 24)
+#define C_HILITE        NGUI_RGB(255, 255, 0)
+
+enum {
+	SRC_NONE = 0,
+	SRC_TABLEAU,
+	SRC_WASTE,
+	SRC_FOUNDATION
+};
+
+enum {
+	TGT_NONE = 0,
+	TGT_TABLEAU,
+	TGT_FOUNDATION
+};
+
+enum {
+	HIT_NONE = 0,
+	HIT_STOCK,
+	HIT_WASTE,
+	HIT_TABLEAU,
+	HIT_FOUNDATION
+};
+
+enum {
+	MENU_NONE = 0,
+	MENU_GAME,
+	MENU_HELP
+};
+
+enum {
+	HELP_NONE = 0,
+	HELP_HOWTO,
+	HELP_ABOUT
+};
+
+enum {
+	ACT_NONE = 0,
+	ACT_NEW,
+	ACT_DRAW1,
+	ACT_DRAW3,
+	ACT_BLUE,
+	ACT_RED,
+	ACT_HOWTO,
+	ACT_ABOUT
+};
+
+#define ID_MENU_GAME  0x101u
+#define ID_MENU_HELP  0x102u
+#define ID_MENU_ITEM  0x120u
+#define ID_HELP_CLOSE 0x140u
+#define ID_HELP_X     0x141u
+
+#define BOUNCE_MAX 18
+
+struct card_pile {
+	unsigned char id[52];
+	unsigned char up[52];
+	int n;
+};
+
+struct sol_layout {
+	int cw, ch;
+	int gap;
+	int col_x[7];
+	int top_y;
+	int tab_y;
+	int status_y;
+};
+
+struct sel {
+	int kind;
+	int pile;
+	int index;
+};
+
+struct target {
+	int kind;
+	int pile;
+};
+
+struct hit {
+	int kind;
+	int pile;
+	int index;
+};
+
+struct menu_entry {
+	int action;
+	const char *label;
+	const char *shortcut;
+};
+
+struct bounce_card {
+	int active;
+	int settled;
+	int id;
+	int x, y, vx, vy;
+};
+
+static struct card_pile stock;
+static struct card_pile waste;
+static struct card_pile foundation[FOUNDATION_PILES];
+static struct card_pile tableau[TABLEAU_PILES];
+
+static int draw_three;
+static int deck_style;
+static int score;
+static int started;
+static int won;
+static int quit;
+
+static unsigned int start_ms;
+static unsigned int end_ms;
+static unsigned int rng_state;
+static unsigned int game_seq;
+
+static struct sel select_sel;
+static struct sel press_sel;
+static struct sel drag_sel;
+static struct target press_target;
+static int press_active;
+static int drag_active;
+static int gesture_consumed;
+static int press_x, press_y;
+static int press_card_x, press_card_y;
+static int drag_dx, drag_dy;
+
+static unsigned int last_click_ms;
+static struct sel last_click_sel;
+static int last_click_valid;
+
+static int menu_open;
+static int help_open;
+static int pending_action;
+static int last_sec = -1;
+
+static struct bounce_card bounce_cards[BOUNCE_MAX];
+static int bounce_count;
+static int bounce_active;
+static int bounce_show;
+static unsigned int bounce_start_ms;
+static unsigned int bounce_spawn_ms;
+
+static const struct menu_entry game_menu[] = {
+	{ ACT_NEW,    "New game",   "N" },
+	{ ACT_NONE,   0,            0   },
+	{ ACT_DRAW1,  "Draw one",   0   },
+	{ ACT_DRAW3,  "Draw three", 0   },
+	{ ACT_NONE,   0,            0   },
+	{ ACT_BLUE,   "Blue deck",  0   },
+	{ ACT_RED,    "Red deck",   0   }
+};
+
+static const struct menu_entry help_menu[] = {
+	{ ACT_HOWTO, "How to play",    0 },
+	{ ACT_ABOUT, "About Solitaire", 0 }
+};
+
+static const char *const howto_lines[] = {
+	"Goal: build each suit from Ace to King.",
+	"Drag cards to move them.",
+	"Drop a card on one rank higher and of",
+	"the opposite colour.",
+	"Empty tableau piles accept a King.",
+	"Double-click a card to send it to a",
+	"foundation.",
+	"Click the deck to draw; click an empty",
+	"deck to recycle the waste.",
+	"N: new game      Esc: close"
+};
+
+static const char *const about_lines[] = {
+	"Nebs OS edition",
+	"",
+	"A Windows 95 style Klondike.",
+	"Drag, drop, and bounce."
+};
+
+/* ------------------------------------------------------------------ */
+/* small helpers                                                      */
+
+static struct ngui_rect rect_make(int x, int y, int w, int h)
+{
+	struct ngui_rect r;
+
+	r.x = x;
+	r.y = y;
+	r.w = w;
+	r.h = h;
+	return r;
+}
+
+static int point_in(const struct ngui_rect *r, int x, int y)
+{
+	return x >= r->x && y >= r->y &&
+	       x < r->x + r->w && y < r->y + r->h;
+}
+
+static void setup_theme(struct ngui *ui)
+{
+	ui->theme.panel = C_MENU;
+	ui->theme.panel_alt = C_MENU_HOT;
+	ui->theme.border = C_MENU_DARK;
+	ui->theme.text = C_TEXT;
+	ui->theme.text_dim = NGUI_RGB(88, 88, 88);
+	ui->theme.accent = C_NAVY;
+	ui->theme.selection = C_NAVY;
+	ui->theme.button = C_MENU;
+	ui->theme.button_hot = C_MENU_HOT;
+	ui->theme.button_down = NGUI_RGB(160, 160, 160);
+	ui->theme.error = C_RED;
+}
+
+static int clampi(int v, int lo, int hi)
+{
+	if (v < lo)
+		return lo;
+	if (v > hi)
+		return hi;
+	return v;
+}
+
+static int imin(int a, int b)
+{
+	return a < b ? a : b;
+}
+
+static int imax(int a, int b)
+{
+	return a > b ? a : b;
+}
+
+static int card_suit(int id)
+{
+	return id / RANK_COUNT;
+}
+
+static int card_rank(int id)
+{
+	return id % RANK_COUNT;
+}
+
+static int suit_is_red(int suit)
+{
+	return suit == 1 || suit == 2;
+}
+
+static const char *rank_text(int rank)
+{
+	static const char *const text[RANK_COUNT] = {
+		"A", "2", "3", "4", "5", "6", "7",
+		"8", "9", "10", "J", "Q", "K"
+	};
+
+	if (rank < 0 || rank >= RANK_COUNT)
+		return "?";
+	return text[rank];
+}
+
+static unsigned int text_len(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s && s[n])
+		n++;
+	return n;
+}
+
+static void fmt_int(char *out, int v)
+{
+	char tmp[12];
+	int n = 0, o = 0;
+	unsigned int uv;
+
+	if (v < 0) {
+		out[o++] = '-';
+		uv = (unsigned int)(-v);
+	} else {
+		uv = (unsigned int)v;
+	}
+	do {
+		tmp[n++] = (char)('0' + (uv % 10));
+		uv /= 10;
+	} while (uv);
+	while (n > 0)
+		out[o++] = tmp[--n];
+	out[o] = '\0';
+}
+
+static unsigned int rng_next(void)
+{
+	unsigned int x = rng_state;
+
+	x ^= x << 13;
+	x ^= x >> 17;
+	x ^= x << 5;
+	rng_state = x ? x : 0x6D2B79F5u;
+	return rng_state;
+}
+
+static void frame2(struct ngui *ui, struct ngui_rect r, unsigned short color)
+{
+	ngui_frame(ui, r, color);
+	if (r.w > 4 && r.h > 4) {
+		r.x++;
+		r.y++;
+		r.w -= 2;
+		r.h -= 2;
+		ngui_frame(ui, r, color);
+	}
+}
+
+static void fill_circle(struct ngui *ui, int cx, int cy, int r,
+			unsigned short color)
+{
+	int dy;
+
+	if (r < 1)
+		r = 1;
+	for (dy = -r; dy <= r; dy++) {
+		int dx = 0;
+
+		while ((dx + 1) * (dx + 1) + dy * dy <= r * r)
+			dx++;
+		if (dx > 0)
+			ngui_fill_rect(ui,
+				       rect_make(cx - dx, cy + dy,
+						 dx * 2 + 1, 1), color);
+	}
+}
+
+/* ------------------------------------------------------------------ */
+/* layout                                                             */
+
+static void compute_layout(struct ngui *ui, struct sol_layout *l)
+{
+	int margin, gap, cw, ch, max_ch, total, i;
+
+	margin = clampi(ui->w / 64, 6, 16);
+	gap = clampi(ui->w / 110, 4, 10);
+	cw = (ui->w - 2 * margin - 6 * gap) / 7;
+	cw = clampi(cw, 14, 92);
+	ch = cw * 135 / 100;
+
+	max_ch = (ui->h - MENU_H - STATUS_H - 30) / 2;
+	if (max_ch < 18)
+		max_ch = 18;
+	if (ch > max_ch) {
+		ch = max_ch;
+		cw = ch * 100 / 135;
+		if (cw < 14)
+			cw = 14;
+	}
+
+	total = 7 * cw + 6 * gap;
+	if (total > ui->w - 2) {
+		if (gap > 2)
+			gap = 2;
+		total = 7 * cw + 6 * gap;
+		if (total > ui->w - 2) {
+			cw = (ui->w - 2 - 6 * gap) / 7;
+			if (cw < 10)
+				cw = 10;
+			total = 7 * cw + 6 * gap;
+		}
+	}
+
+	l->cw = cw;
+	l->ch = ch;
+	l->gap = gap;
+	for (i = 0; i < 7; i++)
+		l->col_x[i] = (ui->w - total) / 2 + i * (cw + gap);
+	l->top_y = MENU_H + 8;
+	l->tab_y = l->top_y + ch + 18;
+	l->status_y = ui->h - STATUS_H;
+	if (l->status_y < MENU_H + 20)
+		l->status_y = MENU_H + 20;
+	if (l->tab_y + ch > l->status_y - 2) {
+		l->tab_y = l->status_y - ch - 2;
+		if (l->tab_y < l->top_y + 4)
+			l->tab_y = l->top_y + 4;
+	}
+}
+
+static struct ngui_rect stock_rect(const struct sol_layout *l)
+{
+	return rect_make(l->col_x[0], l->top_y, l->cw, l->ch);
+}
+
+static struct ngui_rect waste_slot_rect(const struct sol_layout *l)
+{
+	return rect_make(l->col_x[1], l->top_y, l->cw, l->ch);
+}
+
+static struct ngui_rect foundation_rect(const struct sol_layout *l, int f)
+{
+	return rect_make(l->col_x[3 + f], l->top_y, l->cw, l->ch);
+}
+
+static struct ngui_rect tableau_slot_rect(const struct sol_layout *l, int p)
+{
+	return rect_make(l->col_x[p], l->tab_y, l->cw, l->ch);
+}
+
+static int waste_fan(const struct sol_layout *l)
+{
+	return imax(4, l->cw / 5);
+}
+
+static void pile_metrics(const struct card_pile *p,
+			 const struct sol_layout *l, int *fd, int *fu)
+{
+	int down = 0, up, extra, base_extra;
+
+	while (down < p->n && !p->up[down])
+		down++;
+	up = p->n - down;
+
+	*fd = imax(4, l->ch / 10);
+	*fu = imax(9, l->ch / 4);
+
+	extra = l->status_y - l->tab_y - 2 - l->ch;
+	if (extra < 4)
+		extra = 4;
+	base_extra = down * (*fd) + (up > 0 ? up - 1 : 0) * (*fu);
+	if (base_extra > extra) {
+		*fd = (*fd) * extra / base_extra;
+		*fu = (*fu) * extra / base_extra;
+		if (*fd < 2)
+			*fd = 2;
+		if (*fu < 4)
+			*fu = 4;
+	}
+}
+
+static int pile_y(const struct card_pile *p, const struct sol_layout *l,
+		  int index)
+{
+	int fd, fu, y = l->tab_y, i;
+
+	pile_metrics(p, l, &fd, &fu);
+	for (i = 0; i < index && i < p->n; i++)
+		y += p->up[i] ? fu : fd;
+	return y;
+}
+
+/* ------------------------------------------------------------------ */
+/* selection helpers                                                  */
+
+static int sel_count(const struct sel *s)
+{
+	if (s->kind == SRC_TABLEAU)
+		return tableau[s->pile].n - s->index;
+	if (s->kind == SRC_WASTE || s->kind == SRC_FOUNDATION)
+		return 1;
+	return 0;
+}
+
+static int sel_first_id(const struct sel *s)
+{
+	if (s->kind == SRC_TABLEAU) {
+		if (s->index < 0 || s->index >= tableau[s->pile].n)
+			return -1;
+		return tableau[s->pile].id[s->index];
+	}
+	if (s->kind == SRC_WASTE) {
+		if (waste.n <= 0)
+			return -1;
+		return waste.id[waste.n - 1];
+	}
+	if (s->kind == SRC_FOUNDATION) {
+		if (foundation[s->pile].n <= 0)
+			return -1;
+		return foundation[s->pile].id[foundation[s->pile].n - 1];
+	}
+	return -1;
+}
+
+static int sel_valid(const struct sel *s)
+{
+	int i, id, next;
+
+	if (s->kind == SRC_NONE)
+		return 0;
+	if (s->kind == SRC_TABLEAU) {
+		struct card_pile *p = &tableau[s->pile];
+
+		if (s->pile < 0 || s->pile >= TABLEAU_PILES)
+			return 0;
+		if (s->index < 0 || s->index >= p->n)
+			return 0;
+		if (!p->up[s->index])
+			return 0;
+		for (i = s->index; i < p->n; i++) {
+			if (!p->up[i])
+				return 0;
+			if (i + 1 < p->n) {
+				id = p->id[i];
+				next = p->id[i + 1];
+				if (card_rank(next) != card_rank(id) - 1)
+					return 0;
+				if (suit_is_red(card_suit(next)) ==
+				    suit_is_red(card_suit(id)))
+					return 0;
+			}
+		}
+		return 1;
+	}
+	if (s->kind == SRC_WASTE)
+		return waste.n > 0;
+	if (s->kind == SRC_FOUNDATION)
+		return s->pile >= 0 && s->pile < FOUNDATION_PILES &&
+		       foundation[s->pile].n > 0;
+	return 0;
+}
+
+static int same_sel(const struct sel *a, const struct sel *b)
+{
+	return a->kind == b->kind && a->pile == b->pile &&
+	       a->index == b->index;
+}
+
+static void clear_selection(void)
+{
+	select_sel.kind = SRC_NONE;
+	select_sel.pile = 0;
+	select_sel.index = 0;
+}
+
+static struct ngui_rect sel_rect(const struct sol_layout *l,
+				 const struct sel *s)
+{
+	struct card_pile *p;
+
+	if (s->kind == SRC_TABLEAU) {
+		p = &tableau[s->pile];
+		return rect_make(l->col_x[s->pile],
+				 pile_y(p, l, s->index), l->cw, l->ch);
+	}
+	if (s->kind == SRC_WASTE) {
+		int x = l->col_x[1];
+
+		if (draw_three && waste.n > 1) {
+			int start = waste.n > 3 ? waste.n - 3 : 0;
+			int shown = waste.n - start;
+
+			x += (shown - 1) * waste_fan(l);
+		}
+		return rect_make(x, l->top_y, l->cw, l->ch);
+	}
+	if (s->kind == SRC_FOUNDATION)
+		return foundation_rect(l, s->pile);
+	return rect_make(0, 0, 0, 0);
+}
+
+/* ------------------------------------------------------------------ */
+/* hit testing and drop targets                                       */
+
+static struct hit hit_test(const struct sol_layout *l, int x, int y)
+{
+	struct hit h;
+	int i, f;
+
+	h.kind = HIT_NONE;
+	h.pile = 0;
+	h.index = -1;
+
+	{
+		struct ngui_rect r = stock_rect(l);
+
+		if (point_in(&r, x, y)) {
+			h.kind = HIT_STOCK;
+			return h;
+		}
+	}
+	{
+		struct ngui_rect r = waste_slot_rect(l);
+		int fan = waste_fan(l);
+
+		r.w += 2 * fan;
+		if (point_in(&r, x, y)) {
+			h.kind = HIT_WASTE;
+			h.pile = 0;
+			h.index = waste.n - 1;
+			return h;
+		}
+	}
+	for (f = 0; f < FOUNDATION_PILES; f++) {
+		struct ngui_rect r = foundation_rect(l, f);
+
+		if (point_in(&r, x, y)) {
+			h.kind = HIT_FOUNDATION;
+			h.pile = f;
+			h.index = foundation[f].n - 1;
+			return h;
+		}
+	}
+	if (y >= l->tab_y && y < l->status_y) {
+		for (i = 0; i < TABLEAU_PILES; i++) {
+			struct card_pile *p = &tableau[i];
+
+			if (x < l->col_x[i] || x >= l->col_x[i] + l->cw)
+				continue;
+			h.kind = HIT_TABLEAU;
+			h.pile = i;
+			h.index = -1;
+			if (p->n == 0)
+				return h;
+			{
+				int k;
+
+				for (k = p->n - 1; k >= 0; k--) {
+					int y0 = pile_y(p, l, k);
+					int y1 = (k == p->n - 1) ? y0 + l->ch :
+						 pile_y(p, l, k + 1);
+
+					if (y >= y0 && y < y1) {
+						h.index = k;
+						return h;
+					}
+				}
+			}
+			return h;
+		}
+	}
+	return h;
+}
+
+static struct sel selection_from_hit(const struct hit *h)
+{
+	struct sel s;
+
+	s.kind = SRC_NONE;
+	s.pile = 0;
+	s.index = 0;
+	if (h->kind == HIT_WASTE && waste.n > 0) {
+		s.kind = SRC_WASTE;
+		s.index = waste.n - 1;
+	} else if (h->kind == HIT_FOUNDATION && h->index >= 0) {
+		s.kind = SRC_FOUNDATION;
+		s.pile = h->pile;
+		s.index = foundation[h->pile].n - 1;
+	} else if (h->kind == HIT_TABLEAU && h->index >= 0) {
+		s.kind = SRC_TABLEAU;
+		s.pile = h->pile;
+		s.index = h->index;
+	}
+	if (!sel_valid(&s))
+		s.kind = SRC_NONE;
+	return s;
+}
+
+static struct target target_from_hit(const struct hit *h)
+{
+	struct target t;
+
+	t.kind = TGT_NONE;
+	t.pile = 0;
+	if (h->kind == HIT_FOUNDATION) {
+		t.kind = TGT_FOUNDATION;
+		t.pile = h->pile;
+	} else if (h->kind == HIT_TABLEAU) {
+		t.kind = TGT_TABLEAU;
+		t.pile = h->pile;
+	}
+	return t;
+}
+
+static struct target drop_target_at(const struct sol_layout *l, int x, int y)
+{
+	struct target t;
+	int f, p;
+
+	t.kind = TGT_NONE;
+	t.pile = 0;
+
+	if (y >= l->top_y - 10 && y <= l->top_y + l->ch + 10) {
+		for (f = 0; f < FOUNDATION_PILES; f++) {
+			int x0 = l->col_x[3 + f] - l->gap / 2;
+			int x1 = l->col_x[3 + f] + l->cw + l->gap / 2;
+
+			if (x >= x0 && x < x1) {
+				t.kind = TGT_FOUNDATION;
+				t.pile = f;
+				return t;
+			}
+		}
+	}
+	if (y >= l->tab_y - 8 && y < l->status_y) {
+		for (p = 0; p < TABLEAU_PILES; p++) {
+			int x0 = l->col_x[p] - l->gap / 2;
+			int x1 = l->col_x[p] + l->cw + l->gap / 2;
+
+			if (x >= x0 && x < x1) {
+				t.kind = TGT_TABLEAU;
+				t.pile = p;
+				return t;
+			}
+		}
+	}
+	return t;
+}
+
+static int can_drop(const struct sel *s, const struct target *t)
+{
+	int id, r, suit;
+
+	if (s->kind == SRC_NONE || t->kind == TGT_NONE)
+		return 0;
+	if (!sel_valid(s))
+		return 0;
+	id = sel_first_id(s);
+	if (id < 0)
+		return 0;
+	r = card_rank(id);
+	suit = card_suit(id);
+
+	if (t->kind == TGT_FOUNDATION) {
+		struct card_pile *f;
+
+		if (s->kind == SRC_FOUNDATION)
+			return 0;
+		if (sel_count(s) != 1)
+			return 0;
+		if (t->pile < 0 || t->pile >= FOUNDATION_PILES)
+			return 0;
+		f = &foundation[t->pile];
+		if (f->n == 0)
+			return r == 0;
+		if (card_suit(f->id[f->n - 1]) != suit)
+			return 0;
+		return r == f->n;
+	}
+
+	if (t->kind == TGT_TABLEAU) {
+		struct card_pile *p;
+		int top;
+
+		if (t->pile < 0 || t->pile >= TABLEAU_PILES)
+			return 0;
+		if (s->kind == SRC_TABLEAU && s->pile == t->pile)
+			return 0;
+		p = &tableau[t->pile];
+		if (p->n == 0)
+			return r == 12;
+		if (!p->up[p->n - 1])
+			return 0;
+		top = p->id[p->n - 1];
+		if (card_rank(top) != r + 1)
+			return 0;
+		return suit_is_red(card_suit(top)) != suit_is_red(suit);
+	}
+	return 0;
+}
+
+static struct ngui_rect target_rect(const struct sol_layout *l,
+				    const struct target *t)
+{
+	if (t->kind == TGT_FOUNDATION)
+		return foundation_rect(l, t->pile);
+	if (t->kind == TGT_TABLEAU) {
+		struct card_pile *p = &tableau[t->pile];
+
+		if (p->n > 0)
+			return rect_make(l->col_x[t->pile],
+					 pile_y(p, l, p->n - 1), l->cw, l->ch);
+		return tableau_slot_rect(l, t->pile);
+	}
+	return rect_make(0, 0, 0, 0);
+}
+
+static int hit_within_selection(const struct hit *h, const struct sel *s)
+{
+	if (s->kind == SRC_TABLEAU && h->kind == HIT_TABLEAU &&
+	    h->pile == s->pile && h->index >= s->index)
+		return 1;
+	if (s->kind == SRC_WASTE && h->kind == HIT_WASTE)
+		return 1;
+	if (s->kind == SRC_FOUNDATION && h->kind == HIT_FOUNDATION &&
+	    h->pile == s->pile)
+		return 1;
+	return 0;
+}
+
+/* ------------------------------------------------------------------ */
+/* game actions                                                       */
+
+static void shuffle_deck(unsigned char *deck)
+{
+	int i;
+
+	for (i = DECK_SIZE - 1; i > 0; i--) {
+		int j = (int)(rng_next() % (unsigned int)(i + 1));
+		unsigned char t = deck[i];
+
+		deck[i] = deck[j];
+		deck[j] = t;
+	}
+}
+
+static void new_game(unsigned int now)
+{
+	unsigned char deck[DECK_SIZE];
+	struct card_pile *p;
+	int i, row, pos, f;
+
+	game_seq++;
+	rng_state = (u_ticks_ms() * 2654435761u) ^
+		    (game_seq * 2246822519u) ^ 0x9E3779B9u;
+	if (!rng_state)
+		rng_state = 0x6D2B79F5u;
+
+	for (i = 0; i < DECK_SIZE; i++)
+		deck[i] = (unsigned char)i;
+	shuffle_deck(deck);
+
+	stock.n = 0;
+	waste.n = 0;
+	for (f = 0; f < FOUNDATION_PILES; f++) {
+		foundation[f].n = 0;
+		for (i = 0; i < 52; i++)
+			foundation[f].up[i] = 1;
+	}
+	for (i = 0; i < TABLEAU_PILES; i++) {
+		tableau[i].n = 0;
+		for (f = 0; f < 52; f++)
+			tableau[i].up[f] = 0;
+	}
+
+	pos = 0;
+	for (row = 0; row < TABLEAU_PILES; row++) {
+		for (i = row; i < TABLEAU_PILES; i++) {
+			p = &tableau[i];
+			p->id[p->n] = deck[pos++];
+			p->up[p->n] = 0;
+			p->n++;
+		}
+	}
+	for (i = 0; i < TABLEAU_PILES; i++) {
+		p = &tableau[i];
+		if (p->n > 0)
+			p->up[p->n - 1] = 1;
+	}
+	while (pos < DECK_SIZE) {
+		stock.id[stock.n] = deck[pos++];
+		stock.up[stock.n] = 0;
+		stock.n++;
+	}
+
+	score = 0;
+	started = 1;
+	won = 0;
+	start_ms = now;
+	end_ms = 0;
+	last_sec = -1;
+
+	clear_selection();
+	press_active = 0;
+	drag_active = 0;
+	gesture_consumed = 0;
+	last_click_valid = 0;
+	menu_open = MENU_NONE;
+	help_open = HELP_NONE;
+	pending_action = ACT_NONE;
+
+	bounce_active = 0;
+	bounce_show = 0;
+	bounce_count = 0;
+	for (i = 0; i < BOUNCE_MAX; i++) {
+		bounce_cards[i].active = 0;
+		bounce_cards[i].settled = 0;
+	}
+}
+
+static int foundation_for_id(int id)
+{
+	int f, suit = card_suit(id), rank = card_rank(id);
+
+	for (f = 0; f < FOUNDATION_PILES; f++) {
+		if (foundation[f].n > 0 &&
+		    card_suit(foundation[f].id[foundation[f].n - 1]) == suit)
+			return f;
+	}
+	if (rank == 0) {
+		for (f = 0; f < FOUNDATION_PILES; f++)
+			if (foundation[f].n == 0)
+				return f;
+	}
+	return -1;
+}
+
+static void start_bounce(unsigned int now)
+{
+	int i;
+
+	for (i = 0; i < BOUNCE_MAX; i++) {
+		bounce_cards[i].active = 0;
+		bounce_cards[i].settled = 0;
+	}
+	bounce_count = 0;
+	bounce_active = 1;
+	bounce_show = 1;
+	bounce_start_ms = now;
+	bounce_spawn_ms = now;
+}
+
+static int check_win(unsigned int now)
+{
+	int f, total = 0;
+
+	for (f = 0; f < FOUNDATION_PILES; f++)
+		total += foundation[f].n;
+	if (total == DECK_SIZE && !won) {
+		won = 1;
+		end_ms = now;
+		clear_selection();
+		start_bounce(now);
+		return 1;
+	}
+	return 0;
+}
+
+static int perform_move(const struct sel *s, const struct target *t,
+			unsigned int now)
+{
+	struct card_pile *src;
+	unsigned char id;
+	int i, count;
+
+	if (!can_drop(s, t))
+		return 0;
+
+	count = sel_count(s);
+	if (count <= 0)
+		return 0;
+
+	if (s->kind == SRC_TABLEAU) {
+		src = &tableau[s->pile];
+		id = src->id[s->index];
+		if (t->kind == TGT_FOUNDATION) {
+			struct card_pile *d = &foundation[t->pile];
+
+			d->id[d->n] = id;
+			d->up[d->n] = 1;
+			d->n++;
+			score += 10;
+		} else {
+			struct card_pile *d = &tableau[t->pile];
+
+			for (i = 0; i < count; i++) {
+				d->id[d->n] = src->id[s->index + i];
+				d->up[d->n] = 1;
+				d->n++;
+			}
+		}
+		src->n = s->index;
+		if (src->n > 0 && !src->up[src->n - 1]) {
+			src->up[src->n - 1] = 1;
+			score += 5;
+		}
+	} else if (s->kind == SRC_WASTE) {
+		id = waste.id[waste.n - 1];
+		waste.n--;
+		if (t->kind == TGT_FOUNDATION) {
+			struct card_pile *d = &foundation[t->pile];
+
+			d->id[d->n] = id;
+			d->up[d->n] = 1;
+			d->n++;
+			score += 10;
+		} else {
+			struct card_pile *d = &tableau[t->pile];
+
+			d->id[d->n] = id;
+			d->up[d->n] = 1;
+			d->n++;
+			score += 5;
+		}
+	} else if (s->kind == SRC_FOUNDATION) {
+		src = &foundation[s->pile];
+		id = src->id[src->n - 1];
+		src->n--;
+		if (t->kind == TGT_TABLEAU) {
+			struct card_pile *d = &tableau[t->pile];
+
+			d->id[d->n] = id;
+			d->up[d->n] = 1;
+			d->n++;
+			score -= 15;
+		}
+	}
+
+	clear_selection();
+	drag_active = 0;
+	last_click_valid = 0;
+	check_win(now);
+	return 1;
+}
+
+static int auto_to_foundation(const struct sel *s, unsigned int now)
+{
+	struct target t;
+	int id, f;
+
+	if (sel_count(s) != 1)
+		return 0;
+	id = sel_first_id(s);
+	if (id < 0)
+		return 0;
+	f = foundation_for_id(id);
+	if (f < 0)
+		return 0;
+	t.kind = TGT_FOUNDATION;
+	t.pile = f;
+	if (!can_drop(s, &t))
+		return 0;
+	return perform_move(s, &t, now);
+}
+
+static void stock_click(unsigned int now)
+{
+	int count;
+
+	(void)now;
+	if (won)
+		return;
+	if (stock.n > 0) {
+		count = draw_three ? 3 : 1;
+		while (count-- > 0 && stock.n > 0) {
+			unsigned char id = stock.id[stock.n - 1];
+
+			stock.n--;
+			waste.id[waste.n] = id;
+			waste.up[waste.n] = 1;
+			waste.n++;
+		}
+	} else if (waste.n > 0) {
+		while (waste.n > 0) {
+			unsigned char id = waste.id[waste.n - 1];
+
+			waste.n--;
+			stock.id[stock.n] = id;
+			stock.up[stock.n] = 0;
+			stock.n++;
+		}
+		score += draw_three ? -20 : -100;
+	}
+	clear_selection();
+	press_active = 0;
+	last_click_valid = 0;
+}
+
+static int elapsed_seconds(unsigned int now)
+{
+	unsigned int end = won ? end_ms : now;
+
+	if (!started)
+		return 0;
+	if ((int)(end - start_ms) < 0)
+		return 0;
+	return (int)((end - start_ms) / 1000u);
+}
+
+/* ------------------------------------------------------------------ */
+/* card drawing                                                       */
+
+static void fill_taper(struct ngui *ui, int cx, int y0, int y1,
+		       int half0, int half1, unsigned short color)
+{
+	int t, y;
+
+	if (y0 > y1) {
+		t = y0;
+		y0 = y1;
+		y1 = t;
+		t = half0;
+		half0 = half1;
+		half1 = t;
+	}
+	if (y1 <= y0)
+		return;
+	for (y = y0; y <= y1; y++) {
+		int half = half0 + (half1 - half0) * (y - y0) / (y1 - y0);
+
+		if (half > 0)
+			ngui_fill_rect(ui,
+				       rect_make(cx - half, y, half * 2 + 1, 1),
+				       color);
+	}
+}
+
+/* The familiar suit shapes.  rot=1 flips the glyph vertically for the
+ * upside-down pip above a card's bottom-right index. */
+static void draw_suit(struct ngui *ui, int cx, int cy, int suit,
+		      unsigned short color, int size, int rot)
+{
+	int s = size;
+
+	if (s < 7)
+		s = 7;
+
+	if (suit == 0) {
+		/* spade: point up, rounded lobes below, small stem */
+		int r = imax(2, s / 5);
+
+		fill_circle(ui, cx - s / 5, cy + (rot ? -s / 8 : s / 8),
+			    r, color);
+		fill_circle(ui, cx + s / 5, cy + (rot ? -s / 8 : s / 8),
+			    r, color);
+		if (rot)
+			fill_taper(ui, cx, cy + s / 2, cy - s / 6,
+				   0, s / 2, color);
+		else
+			fill_taper(ui, cx, cy - s / 2, cy + s / 6,
+				   0, s / 2, color);
+		if (rot) {
+			ngui_fill_rect(ui, rect_make(cx - s / 12,
+						     cy - s / 5 - s / 5,
+						     s / 6 + 1, s / 5),
+				       color);
+			ngui_fill_rect(ui, rect_make(cx - s / 5, cy - s / 2,
+						     s * 2 / 5, 3), color);
+		} else {
+			ngui_fill_rect(ui, rect_make(cx - s / 12, cy + s / 5,
+						     s / 6 + 1, s / 5),
+				       color);
+			ngui_fill_rect(ui, rect_make(cx - s / 5,
+						     cy + s / 2 - 2,
+						     s * 2 / 5, 3), color);
+		}
+	} else if (suit == 1) {
+		/* heart */
+		int r = imax(2, s / 4);
+		int lobe_y = cy + (rot ? s / 6 : -s / 6);
+
+		fill_circle(ui, cx - s / 5, lobe_y, r, color);
+		fill_circle(ui, cx + s / 5, lobe_y, r, color);
+		if (rot)
+			fill_taper(ui, cx, cy - s / 2, lobe_y - r / 2,
+				   0, s * 45 / 100, color);
+		else
+			fill_taper(ui, cx, lobe_y + r / 2, cy + s / 2,
+				   s * 45 / 100, 0, color);
+	} else if (suit == 2) {
+		/* diamond */
+		int hh = s / 2;
+		int hw = s * 3 / 8;
+		int y;
+
+		for (y = -hh; y <= hh; y++) {
+			int dy = y < 0 ? -y : y;
+			int half = hw * (hh - dy) / hh;
+
+			if (half > 0)
+				ngui_fill_rect(ui,
+					       rect_make(cx - half, cy + y,
+							 half * 2 + 1, 1),
+					       color);
+		}
+	} else {
+		/* club */
+		int r = imax(2, s / 5);
+
+		fill_circle(ui, cx, cy + (rot ? s / 4 : -s / 4), r, color);
+		fill_circle(ui, cx - s / 4, cy + (rot ? -s / 10 : s / 10),
+			    r, color);
+		fill_circle(ui, cx + s / 4, cy + (rot ? -s / 10 : s / 10),
+			    r, color);
+		if (rot) {
+			ngui_fill_rect(ui, rect_make(cx - s / 12,
+						     cy - s / 5 - s / 5,
+						     s / 6 + 1, s / 5),
+				       color);
+			ngui_fill_rect(ui, rect_make(cx - s / 5, cy - s / 2,
+						     s * 2 / 5, 3), color);
+		} else {
+			ngui_fill_rect(ui, rect_make(cx - s / 12, cy + s / 5,
+						     s / 6 + 1, s / 5),
+				       color);
+			ngui_fill_rect(ui, rect_make(cx - s / 5,
+						     cy + s / 2 - 2,
+						     s * 2 / 5, 3), color);
+		}
+	}
+}
+
+static int card_notch(const struct ngui_rect *r)
+{
+	int n = r->w / 30;
+
+	if (n < 2)
+		n = 2;
+	if (n > 4)
+		n = 4;
+	return n;
+}
+
+static struct ngui_rect inset_rect(struct ngui_rect r, int n)
+{
+	if (r.w > 2 * n && r.h > 2 * n) {
+		r.x += n;
+		r.y += n;
+		r.w -= 2 * n;
+		r.h -= 2 * n;
+	}
+	return r;
+}
+
+/* White playing cards have a small square cut out of each corner.  Filling a
+ * plain rectangle would paint over the card underneath, so build the shape
+ * from three bands. */
+static void fill_card_shape(struct ngui *ui, struct ngui_rect r,
+			    unsigned short color, int notch)
+{
+	if (notch <= 0 || r.w <= 2 * notch || r.h <= 2 * notch) {
+		ngui_fill_rect(ui, r, color);
+		return;
+	}
+	ngui_fill_rect(ui, rect_make(r.x + notch, r.y,
+				     r.w - 2 * notch, notch), color);
+	ngui_fill_rect(ui, rect_make(r.x, r.y + notch,
+				     r.w, r.h - 2 * notch), color);
+	ngui_fill_rect(ui, rect_make(r.x + notch, r.y + r.h - notch,
+				     r.w - 2 * notch, notch), color);
+}
+
+struct pip_pos {
+	unsigned short x, y;
+};
+
+static const struct pip_pos pips2[] = {
+	{ 500, 260 }, { 500, 740 }
+};
+static const struct pip_pos pips3[] = {
+	{ 500, 260 }, { 500, 500 }, { 500, 740 }
+};
+static const struct pip_pos pips4[] = {
+	{ 320, 260 }, { 680, 260 }, { 320, 740 }, { 680, 740 }
+};
+static const struct pip_pos pips5[] = {
+	{ 320, 260 }, { 680, 260 }, { 500, 500 },
+	{ 320, 740 }, { 680, 740 }
+};
+static const struct pip_pos pips6[] = {
+	{ 320, 260 }, { 680, 260 }, { 320, 500 }, { 680, 500 },
+	{ 320, 740 }, { 680, 740 }
+};
+static const struct pip_pos pips7[] = {
+	{ 320, 260 }, { 680, 260 }, { 500, 380 },
+	{ 320, 500 }, { 680, 500 }, { 320, 740 }, { 680, 740 }
+};
+static const struct pip_pos pips8[] = {
+	{ 320, 260 }, { 680, 260 }, { 500, 380 }, { 320, 500 },
+	{ 680, 500 }, { 500, 620 }, { 320, 740 }, { 680, 740 }
+};
+static const struct pip_pos pips9[] = {
+	{ 320, 220 }, { 680, 220 }, { 320, 390 }, { 680, 390 },
+	{ 500, 500 }, { 320, 610 }, { 680, 610 },
+	{ 320, 780 }, { 680, 780 }
+};
+static const struct pip_pos pips10[] = {
+	{ 320, 220 }, { 680, 220 }, { 320, 390 }, { 680, 390 },
+	{ 500, 305 }, { 500, 695 }, { 320, 610 }, { 680, 610 },
+	{ 320, 780 }, { 680, 780 }
+};
+
+static void draw_pips(struct ngui *ui, const struct ngui_rect *r, int rank,
+		      int suit, unsigned short ink)
+{
+	const struct pip_pos *p = 0;
+	int n = 0, i, ps;
+
+	switch (rank) {
+	case 1: p = pips2; n = 2; break;
+	case 2: p = pips3; n = 3; break;
+	case 3: p = pips4; n = 4; break;
+	case 4: p = pips5; n = 5; break;
+	case 5: p = pips6; n = 6; break;
+	case 6: p = pips7; n = 7; break;
+	case 7: p = pips8; n = 8; break;
+	case 8: p = pips9; n = 9; break;
+	case 9: p = pips10; n = 10; break;
+	default: return;
+	}
+	ps = clampi(r->w * 20 / 100, 7, 20);
+	for (i = 0; i < n; i++) {
+		int cx = r->x + (int)r->w * p[i].x / 1000;
+		int cy = r->y + (int)r->h * p[i].y / 1000;
+
+		draw_suit(ui, cx, cy, suit, ink, ps, 0);
+	}
+}
+
+static void draw_card_corner(struct ngui *ui, const struct ngui_rect *r,
+			     int suit, unsigned short ink,
+			     const char *rt, int rl, int top)
+{
+	int cs = clampi(r->w / 8, 7, 12);
+	int x, y, cx, cy;
+
+	if (top) {
+		x = r->x + 3;
+		y = r->y + 2;
+		ngui_text_styled(ui, x, y, rt, (unsigned int)rl, ink, 1, 1);
+		cx = x + (rl * 8) / 2;
+		cy = y + 17 + cs / 2;
+		if (cy + cs / 2 > r->y + r->h - 2)
+			cy = r->y + r->h - 2 - cs / 2;
+		draw_suit(ui, cx, cy, suit, ink, cs, 0);
+	} else {
+		x = r->x + r->w - 3 - rl * 8;
+		y = r->y + r->h - 18;
+		if (y < r->y + 20)
+			y = r->y + 20;
+		ngui_text_rot180(ui, x, y, rt, (unsigned int)rl, ink);
+		cx = r->x + r->w - 4 - cs / 2;
+		cy = y - 2 - cs / 2;
+		if (cy - cs / 2 < r->y + 2)
+			cy = r->y + 2 + cs / 2;
+		draw_suit(ui, cx, cy, suit, ink, cs, 1);
+	}
+}
+
+static const char *const court_king[48] = {
+	"........YYY....YYY....YYY.......",
+	"........YYY....YYY....YYY.......",
+	"........YYY....YYY....YYY.......",
+	"........#################.......",
+	"........#YYRRYYYRRYYYRRY#.......",
+	"........#################.......",
+	".........HHHHHHHHHHHHHH.........",
+	".........HHHHHHHHHHHHHH.........",
+	"........HHHHHHHHHHHHHHHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSS##SSSS##SSHH........",
+	"........HHSS##SSSS##SSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSggggSggggSHH........",
+	"........HHSSggggRggggSHH........",
+	"........Hggggggggggggggg........",
+	"........Hggggggggggggggg........",
+	"........Hggggggggggggggg........",
+	".........ggggggggggggggg........",
+	".........ggggggggggggggg........",
+	"..........############..........",
+	"..........#WWWWWWWWWW#..........",
+	"..........#WWWWWWWWWW#..........",
+	"..........############..........",
+	"........#RRRRRRRRRRRRRRR#.......",
+	"........#RYRRRRRRRRRRRYR#.......",
+	"....#####YRRRRRRRRRRRRRY#####...",
+	"....#RRR#YRRRRRRRRRRRRRY#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#YYYYYRRRRRRRRRRRRRYYYYY#...",
+	"...##YYYYYRRRRRRRRRRRRRYYYYY##..",
+	"...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
+	"...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
+	"..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
+	".#R####R################RR####R#",
+	".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"RRRRRR####################RRRRRR",
+	"......#RRRRRRRRRRRRRRRRRR#......",
+	"......####################......",
+	"................................",
+};
+
+static const char *const court_queen[48] = {
+	"..........YWY..YWY..YWY.........",
+	"..........YYY..YYY..YYY.........",
+	"..........YYY..YYY..YYY.........",
+	".........#YYY#RRRR##YYY#........",
+	".........#YYYYRRRRYYYYY#........",
+	".........###############........",
+	".........HHHHHHHHHHHHHH.........",
+	".........HHHHHHHHHHHHHH.........",
+	".......HHHHHHHHHHHHHHHHHH.......",
+	".......HHHSSSSSSSSSSSSHHH.......",
+	".......HHHSSSSSSSSSSSSHHH.......",
+	".......HHHSSSSSSSSSSSSHHH.......",
+	".......HHHSS##SSSS##SSHHH.......",
+	".......HHHSS##SSSS##SSHHH.......",
+	".......HHHSSSSS##SSSSSHHH.......",
+	".......HHHSSSSS##SSSSSHHH.......",
+	".......HHHSSSSS##SSSSSHHH.......",
+	".......HHHSSSSSSSSSSSSHHH.......",
+	".......HHHSSSRRRRRRSSSHHH.......",
+	".......HHHSSSRRRRRRSSSHHH.......",
+	".......HHHSSSSSSSSSSSSHHH.......",
+	".......HHH############HHH.......",
+	".......HHH..SSSSSSSS..HHH.......",
+	".......HHH..SSSSSSSS..HHH.......",
+	".......HHH############HHH.......",
+	".......HHH#WWWWWWWWWW#HHH.......",
+	".......HHH#WWWWWWWWWW#HHH.......",
+	".......HHH############HHH.......",
+	"........#RRRRRRRRRRRRRRR#.......",
+	"........#RYRRRRRRRRRRRYR#.......",
+	"....#####YRRRRRRRRRRRRRY#####...",
+	"....#RRR#YRRRRRRRRRRRRRY#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#YYYYYRRRRRRRRRRRRRYYYYY#...",
+	"...##YYYYYRRRRRRRRRRRRRYYYYY##..",
+	"...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
+	"...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
+	"..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
+	".#R####R################RR####R#",
+	".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"RRRRRR####################RRRRRR",
+	"......#RRRRRRRRRRRRRRRRRR#......",
+	"......####################......",
+	"................................",
+};
+
+static const char *const court_jack[48] = {
+	".......................RRY......",
+	".......................RRRY.....",
+	"........###############RRR......",
+	"........#RRRRRRRRRRRRRRRRR......",
+	"........#RRRRRRRRRRRRRRRRR......",
+	"........#RRRRRRRRRRRRRRR#.......",
+	"........#RRRRRRRRRRRRRRR#.......",
+	"........YYYYYYYYYYYYYYYYY.......",
+	"........HHHHHHHHHHHHHHHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSS##SSSS##SSHH........",
+	"........HHSS##SSSS##SSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSSSS##SSSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HHSSSRRRRRRSSSHH........",
+	"........HHSSSRRRRRRSSSHH........",
+	"........HHSSSSSSSSSSSSHH........",
+	"........HH############HH........",
+	"............SSSSSSSS............",
+	"............SSSSSSSS............",
+	"..........############..........",
+	"..........#WWWWWWWWWW#..........",
+	"..........#WWWWYYWWWW#..........",
+	"..........#####YY#####..........",
+	"........#RRRRRRRRRRRRRRR#.......",
+	"........#RYRRRRRRRRRRRYR#.......",
+	"....#####YRRRRRRRRRRRRRY#####...",
+	"....#RRR#YRRRRRRRRRRRRRY#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#RRR#RRRRRRRRRRRRRRR#RRR#...",
+	"....#YYYYYRRRRRRRRRRRRRYYYYY#...",
+	"...##YYYYYRRRRRRRRRRRRRYYYYY##..",
+	"...#SS#RRRRRRRRRRRRRRRRRRR#SS#..",
+	"...#SS#R#YYYYYRRRRYYYYY#RR#SS#..",
+	"..##SS#R#YYYYYRRRRYYYYY#RR#SS##.",
+	".#R####R################RR####R#",
+	".#RYRRRRRRRRRRRRRRRRRRRRRRRRRYR#",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"#RYRRRRRRRRRRRRRRRRRRRRRRRRRRRYR",
+	"RRRRRR####################RRRRRR",
+	"......#RRRRRRRRRRRRRRRRRR#......",
+	"......####################......",
+	"................................",
+};
+
+static const char *const *court_rows(int rank)
+{
+	if (rank == 12)
+		return court_king;
+	if (rank == 11)
+		return court_queen;
+	if (rank == 10)
+		return court_jack;
+	return 0;
+}
+
+static unsigned short court_color(char c)
+{
+	switch (c) {
+	case '#': return C_BLACK;
+	case 'W': return C_CARD;
+	case 'S': return C_SKIN;
+	case 'H': return C_HAIR;
+	case 'Y': return C_GOLD;
+	case 'R': return C_RED;
+	case 'g': return NGUI_RGB(184, 184, 184);
+	default: return 0;
+	}
+}
+
+/* Blit the 32x48 court portrait with one fill per same-colour run. */
+static void draw_court_bitmap(struct ngui *ui, int px, int py, int pw, int ph,
+			      int rank)
+{
+	const char *const *rows = court_rows(rank);
+	int x, y;
+
+	if (!rows)
+		return;
+	for (y = 0; y < 48; y++) {
+		int y0 = py + y * ph / 48;
+		int y1 = py + (y + 1) * ph / 48;
+
+		for (x = 0; x < 32; ) {
+			char ch = rows[y][x];
+			int x2, x0, x1;
+			unsigned short color;
+
+			if (ch == '.') {
+				x++;
+				continue;
+			}
+			x2 = x;
+			while (x2 + 1 < 32 && rows[y][x2 + 1] == ch)
+				x2++;
+			color = court_color(ch);
+			if (color) {
+				x0 = px + x * pw / 32;
+				x1 = px + (x2 + 1) * pw / 32;
+				ngui_fill_rect(ui, rect_make(x0, y0,
+							     x1 - x0, y1 - y0),
+					       color);
+			}
+			x = x2 + 1;
+		}
+	}
+}
+
+static void draw_court_card(struct ngui *ui, const struct ngui_rect *r,
+			    int rank, int suit, unsigned short ink)
+{
+	int px = r->x + r->w * 19 / 100;
+	int py = r->y + r->h * 11 / 100;
+	int pw = r->w * 62 / 100;
+	int ph = r->h * 78 / 100;
+
+	if (pw < 24 || ph < 40) {
+		int ss = imin(r->w * 45 / 100, r->h * 30 / 100);
+
+		ss = clampi(ss, 9, 40);
+		draw_suit(ui, r->x + r->w / 2, r->y + r->h / 2 + 2,
+			  suit, ink, ss, 0);
+		return;
+	}
+	ngui_fill_rect(ui, rect_make(px, py, pw, ph), C_CREAM);
+	draw_court_bitmap(ui, px, py, pw, ph, rank);
+	ngui_frame(ui, rect_make(px, py, pw, ph), C_BLACK);
+}
+
+static void draw_card_face_content(struct ngui *ui, struct ngui_rect r, int id)
+{
+	int rank = card_rank(id);
+	int suit = card_suit(id);
+	unsigned short ink = suit_is_red(suit) ? C_RED : C_BLACK;
+	const char *rt = rank_text(rank);
+	int rl = (int)text_len(rt);
+	int notch = card_notch(&r);
+
+	fill_card_shape(ui, r, C_CARD_EDGE, notch);
+	fill_card_shape(ui, inset_rect(r, 1), C_CARD, imax(0, notch - 1));
+	ngui_push_clip(ui, r);
+
+	if (rank >= 10) {
+		draw_court_card(ui, &r, rank, suit, ink);
+	} else if (rank == 0) {
+		int ss = imin(r.w * 45 / 100, r.h * 30 / 100);
+
+		ss = clampi(ss, 9, 40);
+		draw_suit(ui, r.x + r.w / 2, r.y + r.h / 2 + 2,
+			  suit, ink, ss, 0);
+	} else if (r.w < 30) {
+		int ss = imin(r.w * 42 / 100, r.h * 28 / 100);
+
+		ss = clampi(ss, 8, 30);
+		draw_suit(ui, r.x + r.w / 2, r.y + r.h / 2 + 2,
+			  suit, ink, ss, 0);
+	} else {
+		draw_pips(ui, &r, rank, suit, ink);
+	}
+
+	draw_card_corner(ui, &r, suit, ink, rt, rl, 1);
+	draw_card_corner(ui, &r, suit, ink, rt, rl, 0);
+	ngui_pop_clip(ui);
+}
+
+/* The Windows Solitaire back is a little castle on a night hill. */
+static void draw_card_back_content(struct ngui *ui, struct ngui_rect r)
+{
+	struct ngui_rect inner;
+	unsigned short sky, sky2, moon, hill, tower, tower_dark, lit;
+	int notch = card_notch(&r);
+	int mx, my, mr, base, keep_w, keep_h, kx, ky, i, tw, th;
+
+	if (deck_style == 1) {
+		sky = NGUI_RGB(96, 0, 0);
+		sky2 = NGUI_RGB(128, 24, 24);
+		moon = NGUI_RGB(255, 208, 128);
+		hill = NGUI_RGB(48, 0, 0);
+		tower = NGUI_RGB(224, 150, 128);
+		tower_dark = NGUI_RGB(104, 40, 40);
+		lit = NGUI_RGB(255, 232, 144);
+	} else {
+		sky = NGUI_RGB(0, 0, 96);
+		sky2 = NGUI_RGB(0, 28, 128);
+		moon = NGUI_RGB(236, 236, 168);
+		hill = NGUI_RGB(0, 0, 48);
+		tower = NGUI_RGB(184, 196, 220);
+		tower_dark = NGUI_RGB(56, 68, 112);
+		lit = NGUI_RGB(255, 232, 144);
+	}
+
+	fill_card_shape(ui, r, C_CARD_EDGE, notch);
+	fill_card_shape(ui, inset_rect(r, 1), C_CARD, imax(0, notch - 1));
+	inner = inset_rect(r, 3);
+	if (inner.w < 6 || inner.h < 6)
+		return;
+
+	ngui_fill_rect(ui, inner, sky);
+	ngui_fill_rect(ui, rect_make(inner.x, inner.y + inner.h * 58 / 100,
+				     inner.w, inner.h * 42 / 100), sky2);
+
+	mx = inner.x + inner.w * 74 / 100;
+	my = inner.y + inner.h * 22 / 100;
+	mr = imax(2, inner.w / 10);
+	fill_circle(ui, mx, my, mr, moon);
+	fill_circle(ui, mx + mr / 2, my - mr / 3, mr * 4 / 5, sky);
+
+	{
+		static const unsigned char star_x[] = { 18, 34, 48, 62, 24, 84 };
+		static const unsigned char star_y[] = { 12, 30, 16, 34, 48, 40 };
+
+		for (i = 0; i < 6; i++) {
+			int sx = inner.x + inner.w * star_x[i] / 100;
+			int sy = inner.y + inner.h * star_y[i] / 100;
+
+			ngui_fill_rect(ui, rect_make(sx, sy, 1, 1), moon);
+		}
+	}
+
+	base = inner.y + inner.h * 76 / 100;
+	ngui_fill_rect(ui, rect_make(inner.x, base, inner.w,
+				     inner.y + inner.h - base), hill);
+
+	if (inner.w < 26 || inner.h < 34) {
+		ngui_fill_rect(ui, rect_make(inner.x + inner.w / 2 - 2,
+					     base - inner.h / 4, 5,
+					     inner.h / 4), tower);
+		ngui_frame(ui, inner, tower_dark);
+		return;
+	}
+
+	/* central keep */
+	keep_w = inner.w * 34 / 100;
+	keep_h = inner.h * 34 / 100;
+	kx = inner.x + (inner.w - keep_w) / 2;
+	ky = base - keep_h;
+	ngui_fill_rect(ui, rect_make(kx, ky, keep_w, keep_h), tower);
+	ngui_frame(ui, rect_make(kx, ky, keep_w, keep_h), tower_dark);
+	for (i = 0; i < 3; i++)
+		ngui_fill_rect(ui, rect_make(kx + i * keep_w / 3 + 1,
+					     ky - 3, keep_w / 5, 3), tower);
+	ngui_fill_rect(ui, rect_make(kx + keep_w / 2 - 4,
+				     ky + keep_h * 35 / 100, 3, 5), lit);
+	ngui_fill_rect(ui, rect_make(kx + keep_w / 2 + 2,
+				     ky + keep_h * 35 / 100, 3, 5), lit);
+	ngui_fill_rect(ui, rect_make(kx + keep_w / 2 - 4, base - 12,
+				     9, 12), tower_dark);
+
+	/* side towers */
+	tw = inner.w * 18 / 100;
+	th = inner.h * 26 / 100;
+	for (i = 0; i < 2; i++) {
+		int tx = i ? inner.x + inner.w - tw - inner.w * 12 / 100 :
+			     inner.x + inner.w * 12 / 100;
+
+		ngui_fill_rect(ui, rect_make(tx, base - th, tw, th), tower);
+		ngui_frame(ui, rect_make(tx, base - th, tw, th), tower_dark);
+		ngui_fill_rect(ui, rect_make(tx + tw / 2 - 1,
+					     base - th + 4, 3, 4), lit);
+		ngui_fill_rect(ui, rect_make(tx + tw / 4, base - th - 3,
+					     tw / 2, 3), tower);
+	}
+
+	/* flag */
+	ngui_line(ui, kx + keep_w / 2, ky - 10, kx + keep_w / 2, ky - 3,
+		  tower_dark);
+	ngui_fill_rect(ui, rect_make(kx + keep_w / 2 + 1, ky - 10,
+				     5, 3), C_RED);
+
+	ngui_frame(ui, inner, tower_dark);
+}
+
+static void draw_card(struct ngui *ui, struct ngui_rect r, int id, int up)
+{
+	int notch = card_notch(&r);
+
+	fill_card_shape(ui, rect_make(r.x + 2, r.y + 2, r.w, r.h),
+			C_SHADOW, notch);
+	if (up)
+		draw_card_face_content(ui, r, id);
+	else
+		draw_card_back_content(ui, r);
+}
+
+static void draw_empty_slot(struct ngui *ui, struct ngui_rect r)
+{
+	ngui_fill_rect(ui, r, C_SLOT_FILL);
+	ngui_frame(ui, r, C_SLOT_LINE);
+}
+
+static void draw_recycle_icon(struct ngui *ui, struct ngui_rect r)
+{
+	int cx = r.x + r.w / 2;
+	int cy = r.y + r.h / 2;
+	int rad = imin(r.w, r.h) / 5;
+	int q = rad * 7 / 10;
+
+	if (rad < 5)
+		return;
+	ngui_line(ui, cx, cy - rad, cx + q, cy - q, C_SLOT_ICON);
+	ngui_line(ui, cx + q, cy - q, cx + rad, cy, C_SLOT_ICON);
+	ngui_line(ui, cx + rad, cy, cx + q, cy + q, C_SLOT_ICON);
+	ngui_line(ui, cx + q, cy + q, cx, cy + rad, C_SLOT_ICON);
+	ngui_line(ui, cx, cy + rad, cx - q, cy + q, C_SLOT_ICON);
+	ngui_line(ui, cx - q, cy + q, cx - rad, cy, C_SLOT_ICON);
+	ngui_line(ui, cx - rad, cy, cx - q, cy - q, C_SLOT_ICON);
+	ngui_line(ui, cx - q, cy - q, cx - rad / 2, cy - rad, C_SLOT_ICON);
+	ngui_line(ui, cx, cy - rad, cx + rad / 2, cy - rad, C_SLOT_ICON);
+	ngui_line(ui, cx, cy - rad, cx, cy - rad / 2, C_SLOT_ICON);
+}
+
+/* ------------------------------------------------------------------ */
+/* table drawing                                                      */
+
+static void draw_stock(struct ngui *ui, const struct sol_layout *l)
+{
+	struct ngui_rect r = stock_rect(l);
+
+	if (stock.n > 0) {
+		draw_card(ui, r, stock.id[stock.n - 1], 0);
+	} else {
+		draw_empty_slot(ui, r);
+		if (waste.n > 0)
+			draw_recycle_icon(ui, r);
+	}
+}
+
+static void draw_waste(struct ngui *ui, const struct sol_layout *l)
+{
+	struct ngui_rect r = waste_slot_rect(l);
+	int fan = waste_fan(l);
+	int top = waste.n;
+	int start, i;
+
+	if (drag_active && drag_sel.kind == SRC_WASTE)
+		top--;
+
+	if (top <= 0) {
+		draw_empty_slot(ui, r);
+		return;
+	}
+	if (draw_three)
+		start = top > 3 ? top - 3 : 0;
+	else
+		start = top - 1;
+
+	for (i = start; i < top; i++) {
+		struct ngui_rect cr = r;
+
+		cr.x += (i - start) * fan;
+		draw_card(ui, cr, waste.id[i], 1);
+	}
+}
+
+static void draw_foundations(struct ngui *ui, const struct sol_layout *l)
+{
+	int f;
+
+	for (f = 0; f < FOUNDATION_PILES; f++) {
+		struct ngui_rect r = foundation_rect(l, f);
+		int top = foundation[f].n;
+
+		if (drag_active && drag_sel.kind == SRC_FOUNDATION &&
+		    drag_sel.pile == f)
+			top--;
+		if (top > 0)
+			draw_card(ui, r, foundation[f].id[top - 1], 1);
+		else
+			draw_empty_slot(ui, r);
+	}
+}
+
+static void draw_tableau(struct ngui *ui, const struct sol_layout *l)
+{
+	int p, i;
+
+	for (p = 0; p < TABLEAU_PILES; p++) {
+		struct card_pile *pile = &tableau[p];
+		int n = pile->n;
+
+		if (drag_active && drag_sel.kind == SRC_TABLEAU &&
+		    drag_sel.pile == p)
+			n = drag_sel.index;
+		if (n <= 0) {
+			draw_empty_slot(ui, tableau_slot_rect(l, p));
+			continue;
+		}
+		for (i = 0; i < n; i++) {
+			struct ngui_rect r;
+
+			r = rect_make(l->col_x[p], pile_y(pile, l, i),
+				      l->cw, l->ch);
+			draw_card(ui, r, pile->id[i], pile->up[i]);
+		}
+	}
+}
+
+static void draw_drop_highlight(struct ngui *ui, const struct sol_layout *l)
+{
+	const struct sel *s = 0;
+	struct target t;
+	struct ngui_rect r;
+
+	if (drag_active)
+		s = &drag_sel;
+	else if (select_sel.kind != SRC_NONE)
+		s = &select_sel;
+	if (!s)
+		return;
+	t = drop_target_at(l, ui->mouse_x, ui->mouse_y);
+	if (!can_drop(s, &t))
+		return;
+	r = target_rect(l, &t);
+	if (r.w > 0 && r.h > 0)
+		frame2(ui, r, C_HILITE);
+}
+
+static void draw_selection_outline(struct ngui *ui, const struct sol_layout *l)
+{
+	struct ngui_rect r;
+
+	if (drag_active || select_sel.kind == SRC_NONE)
+		return;
+	if (select_sel.kind == SRC_TABLEAU) {
+		struct card_pile *p = &tableau[select_sel.pile];
+		int y0 = pile_y(p, l, select_sel.index);
+		int y1 = pile_y(p, l, p->n - 1) + l->ch;
+
+		r = rect_make(l->col_x[select_sel.pile], y0, l->cw, y1 - y0);
+	} else {
+		r = sel_rect(l, &select_sel);
+	}
+	frame2(ui, r, C_HILITE);
+}
+
+static void draw_drag_cards(struct ngui *ui, const struct sol_layout *l)
+{
+	struct card_pile *p;
+	int count, i, step, x, y;
+
+	if (!drag_active || drag_sel.kind == SRC_NONE)
+		return;
+	count = sel_count(&drag_sel);
+	if (count <= 0)
+		return;
+	x = ui->mouse_x - drag_dx;
+	y = ui->mouse_y - drag_dy;
+	step = imax(8, l->ch / 5);
+	for (i = 0; i < count; i++) {
+		struct ngui_rect r = rect_make(x, y + i * step, l->cw, l->ch);
+
+		if (drag_sel.kind == SRC_TABLEAU) {
+			p = &tableau[drag_sel.pile];
+			draw_card(ui, r, p->id[drag_sel.index + i], 1);
+		} else if (drag_sel.kind == SRC_WASTE) {
+			draw_card(ui, r, waste.id[waste.n - 1], 1);
+		} else {
+			p = &foundation[drag_sel.pile];
+			draw_card(ui, r, p->id[p->n - 1], 1);
+		}
+	}
+}
+
+static void draw_bounce_cards(struct ngui *ui, const struct sol_layout *l)
+{
+	int i;
+
+	if (!bounce_show)
+		return;
+	for (i = 0; i < BOUNCE_MAX; i++) {
+		struct bounce_card *b = &bounce_cards[i];
+
+		if (!b->active && !b->settled)
+			continue;
+		draw_card(ui, rect_make(b->x, b->y, l->cw, l->ch), b->id, 1);
+	}
+}
+
+/* ------------------------------------------------------------------ */
+/* menu, status and help                                              */
+
+static int menu_entries(int which, const struct menu_entry **out)
+{
+	if (which == MENU_GAME) {
+		*out = game_menu;
+		return (int)(sizeof(game_menu) / sizeof(game_menu[0]));
+	}
+	*out = help_menu;
+	return (int)(sizeof(help_menu) / sizeof(help_menu[0]));
+}
+
+static int menu_entry_h(const struct menu_entry *e)
+{
+	return e->label ? 21 : 7;
+}
+
+static int menu_dropdown_h(int which)
+{
+	const struct menu_entry *e;
+	int n, i, h = 6;
+
+	n = menu_entries(which, &e);
+	for (i = 0; i < n; i++)
+		h += menu_entry_h(&e[i]);
+	return h;
+}
+
+static int menu_x(int which)
+{
+	return which == MENU_GAME ? 4 : 52;
+}
+
+static int menu_w(int which)
+{
+	return which == MENU_GAME ? 172 : 158;
+}
+
+static int action_checked(int action)
+{
+	switch (action) {
+	case ACT_DRAW1:
+		return !draw_three;
+	case ACT_DRAW3:
+		return draw_three;
+	case ACT_BLUE:
+		return deck_style == 0;
+	case ACT_RED:
+		return deck_style == 1;
+	default:
+		return 0;
+	}
+}
+
+static int menu_bar_button(struct ngui *ui, unsigned int id,
+			   struct ngui_rect r, const char *label, int active)
+{
+	unsigned short ob = ui->theme.button;
+	unsigned short oh = ui->theme.button_hot;
+	unsigned short od = ui->theme.button_down;
+	unsigned short obd = ui->theme.border;
+	unsigned short ot = ui->theme.text;
+	int fired;
+
+	if (active) {
+		ui->theme.button = C_NAVY;
+		ui->theme.button_hot = C_NAVY;
+		ui->theme.button_down = C_NAVY;
+		ui->theme.border = C_NAVY;
+		ui->theme.text = C_TEXT_LIGHT;
+	} else {
+		ui->theme.button = C_MENU;
+		ui->theme.button_hot = C_MENU_HOT;
+		ui->theme.button_down = NGUI_RGB(160, 160, 160);
+		ui->theme.border = C_MENU;
+		ui->theme.text = C_TEXT;
+	}
+	fired = ngui_button(ui, id, r, label);
+	ui->theme.button = ob;
+	ui->theme.button_hot = oh;
+	ui->theme.button_down = od;
+	ui->theme.border = obd;
+	ui->theme.text = ot;
+	return fired;
+}
+
+static void draw_menu_bar(struct ngui *ui, const struct sol_layout *l)
+{
+	struct ngui_rect gr = rect_make(6, 2, 44, 18);
+	struct ngui_rect hr = rect_make(52, 2, 44, 18);
+
+	(void)l;
+	ngui_fill_rect(ui, rect_make(0, 0, ui->w, MENU_H), C_MENU);
+	ngui_fill_rect(ui, rect_make(0, 0, ui->w, 1), C_MENU_TOP);
+	ngui_fill_rect(ui, rect_make(0, MENU_H - 1, ui->w, 1), C_MENU_DARK);
+
+	if (menu_bar_button(ui, ID_MENU_GAME, gr, "Game",
+			    menu_open == MENU_GAME)) {
+		menu_open = (menu_open == MENU_GAME) ? MENU_NONE : MENU_GAME;
+		ngui_invalidate(ui);
+	}
+	if (menu_bar_button(ui, ID_MENU_HELP, hr, "Help",
+			    menu_open == MENU_HELP)) {
+		menu_open = (menu_open == MENU_HELP) ? MENU_NONE : MENU_HELP;
+		ngui_invalidate(ui);
+	}
+}
+
+static void draw_dropdown(struct ngui *ui)
+{
+	const struct menu_entry *e;
+	int n, i, x, y, w, h, ey;
+
+	if (menu_open == MENU_NONE)
+		return;
+	n = menu_entries(menu_open, &e);
+	x = menu_x(menu_open);
+	y = MENU_H;
+	w = menu_w(menu_open);
+	h = menu_dropdown_h(menu_open);
+
+	ngui_fill_rect(ui, rect_make(x + 2, y + 2, w, h),
+		       NGUI_RGB(96, 96, 96));
+	ngui_fill_rect(ui, rect_make(x, y, w, h), C_MENU);
+	ngui_frame(ui, rect_make(x, y, w, h), C_MENU_DARK);
+	ngui_fill_rect(ui, rect_make(x + 1, y + 1, w - 2, 1), C_MENU_TOP);
+
+	ey = y + 3;
+	for (i = 0; i < n; i++) {
+		int eh = menu_entry_h(&e[i]);
+
+		if (!e[i].label) {
+			ngui_fill_rect(ui, rect_make(x + 4, ey + 3, w - 8, 1),
+				       NGUI_RGB(128, 128, 128));
+			ey += eh;
+			continue;
+		}
+		if (ngui_menu_item(ui, ID_MENU_ITEM + (unsigned int)e[i].action,
+				   rect_make(x + 2, ey, w - 4, eh),
+				   e[i].label, e[i].shortcut,
+				   action_checked(e[i].action))) {
+			pending_action = e[i].action;
+			menu_open = MENU_NONE;
+			ngui_invalidate(ui);
+		}
+		ey += eh;
+	}
+}
+
+static void draw_status(struct ngui *ui, const struct sol_layout *l)
+{
+	char b[32], t[24];
+	int n, y = l->status_y + 2;
+
+	ngui_fill_rect(ui, rect_make(0, l->status_y, ui->w,
+				     ui->h - l->status_y), C_MENU);
+	ngui_fill_rect(ui, rect_make(0, l->status_y, ui->w, 1), C_MENU_TOP);
+	ngui_fill_rect(ui, rect_make(0, ui->h - 1, ui->w, 1), C_MENU_DARK);
+
+	n = 0;
+	b[n++] = 'S';
+	b[n++] = 'c';
+	b[n++] = 'o';
+	b[n++] = 'r';
+	b[n++] = 'e';
+	b[n++] = ':';
+	b[n++] = ' ';
+	fmt_int(b + n, score);
+	ngui_text(ui, 8, y, b, C_TEXT);
+
+	n = 0;
+	t[n++] = 'T';
+	t[n++] = 'i';
+	t[n++] = 'm';
+	t[n++] = 'e';
+	t[n++] = ':';
+	t[n++] = ' ';
+	fmt_int(t + n, elapsed_seconds(u_ticks_ms()));
+	{
+		int tw = (int)text_len(t) * 8;
+
+		ngui_text(ui, ui->w - 8 - tw, y, t, C_TEXT);
+	}
+
+	if (won) {
+		const char *msg = "You win!";
+		int mw = (int)text_len(msg) * 8;
+		int mx = (ui->w - mw) / 2;
+		int left_w = (int)text_len(b) * 8 + 8;
+		int right_w = (int)text_len(t) * 8 + 8;
+
+		if (mx > left_w + 12 && mx + mw < ui->w - right_w - 12)
+			ngui_text(ui, mx, y, msg, C_NAVY);
+	}
+}
+
+static void help_geometry(struct ngui *ui, struct ngui_rect *box,
+			  struct ngui_rect *btn)
+{
+	int w = 470, h = 300;
+
+	if (w > ui->w - 24)
+		w = ui->w - 24;
+	if (w < 280)
+		w = 280;
+	if (h > ui->h - 40)
+		h = ui->h - 40;
+	if (h < 220)
+		h = 220;
+	box->x = (ui->w - w) / 2;
+	box->y = (ui->h - h) / 2;
+	box->w = w;
+	box->h = h;
+	btn->x = box->x + (w - 70) / 2;
+	btn->y = box->y + h - 30;
+	btn->w = 70;
+	btn->h = 22;
+}
+
+static void draw_help(struct ngui *ui)
+{
+	struct ngui_rect box, btn;
+	const char *title = help_open == HELP_ABOUT ?
+			    "About Solitaire" : "How to play";
+	int y, i;
+
+	help_geometry(ui, &box, &btn);
+	ngui_fill_rect(ui, rect_make(box.x + 3, box.y + 3, box.w, box.h),
+		       NGUI_RGB(80, 80, 80));
+	ngui_fill_rect(ui, box, C_MENU);
+	ngui_frame(ui, box, C_TEXT);
+	ngui_fill_rect(ui, rect_make(box.x + 1, box.y + 1, box.w - 2, 22),
+		       C_NAVY);
+	ngui_text(ui, box.x + 8, box.y + 4, title, C_TEXT_LIGHT);
+	if (menu_bar_button(ui, ID_HELP_X,
+			    rect_make(box.x + box.w - 18, box.y + 1, 17, 22),
+			    "x", 1)) {
+		help_open = HELP_NONE;
+		ngui_invalidate(ui);
+	}
+
+	y = box.y + 38;
+	if (help_open == HELP_ABOUT) {
+		ngui_text_styled(ui, box.x + 18, y, "Solitaire", 9,
+				 C_TEXT, 2, 1);
+		y += 36;
+		for (i = 0; i < (int)(sizeof(about_lines) /
+				      sizeof(about_lines[0])); i++) {
+			if (y + 16 > box.y + box.h - 40)
+				break;
+			ngui_text(ui, box.x + 18, y, about_lines[i], C_TEXT);
+			y += 18;
+		}
+	} else {
+		for (i = 0; i < (int)(sizeof(howto_lines) /
+				      sizeof(howto_lines[0])); i++) {
+			if (y + 16 > box.y + box.h - 40)
+				break;
+			ngui_text(ui, box.x + 18, y, howto_lines[i], C_TEXT);
+			y += 18;
+		}
+	}
+
+	if (ngui_button(ui, ID_HELP_CLOSE, btn, "Close")) {
+		help_open = HELP_NONE;
+		ngui_invalidate(ui);
+	}
+}
+
+/* ------------------------------------------------------------------ */
+/* interaction                                                        */
+
+static void menu_action(int action, unsigned int now)
+{
+	switch (action) {
+	case ACT_NEW:
+		new_game(now);
+		break;
+	case ACT_DRAW1:
+		draw_three = 0;
+		break;
+	case ACT_DRAW3:
+		draw_three = 1;
+		break;
+	case ACT_BLUE:
+		deck_style = 0;
+		break;
+	case ACT_RED:
+		deck_style = 1;
+		break;
+	case ACT_HOWTO:
+		help_open = HELP_HOWTO;
+		break;
+	case ACT_ABOUT:
+		help_open = HELP_ABOUT;
+		break;
+	default:
+		break;
+	}
+}
+
+static void game_press(struct ngui *ui, const struct sol_layout *l,
+		       unsigned int now, int *changed)
+{
+	struct hit h;
+	struct sel s;
+	struct ngui_rect sr;
+
+	if (won) {
+		if (bounce_active || bounce_show) {
+			bounce_active = 0;
+			bounce_show = 0;
+			*changed = 1;
+		}
+		return;
+	}
+
+	h = hit_test(l, ui->press_x, ui->press_y);
+	if (h.kind == HIT_STOCK) {
+		stock_click(now);
+		*changed = 1;
+		return;
+	}
+
+	s = selection_from_hit(&h);
+	if (select_sel.kind != SRC_NONE && hit_within_selection(&h, &select_sel))
+		s = select_sel;
+
+	press_sel = s;
+	press_target = target_from_hit(&h);
+	press_x = ui->press_x;
+	press_y = ui->press_y;
+	press_active = 1;
+
+	sr = sel_rect(l, &s);
+	press_card_x = sr.x;
+	press_card_y = sr.y;
+	if (s.kind == SRC_NONE) {
+		press_card_x = ui->press_x;
+		press_card_y = ui->press_y;
+	}
+}
+
+static void select_click(const struct sol_layout *l, unsigned int now,
+			 int *changed)
+{
+	int dbl = 0;
+
+	if (last_click_valid && same_sel(&press_sel, &last_click_sel) &&
+	    (unsigned int)(now - last_click_ms) < 800u)
+		dbl = 1;
+
+	if (dbl && press_sel.kind != SRC_NONE) {
+		if (auto_to_foundation(&press_sel, now)) {
+			*changed = 1;
+			last_click_valid = 0;
+			return;
+		}
+	}
+
+	if (select_sel.kind != SRC_NONE && press_target.kind != TGT_NONE) {
+		int same_source = (select_sel.kind == SRC_TABLEAU &&
+				   press_target.kind == TGT_TABLEAU &&
+				   select_sel.pile == press_target.pile) ||
+				  (select_sel.kind == SRC_FOUNDATION &&
+				   press_target.kind == TGT_FOUNDATION &&
+				   select_sel.pile == press_target.pile);
+
+		if (!same_source) {
+			if (perform_move(&select_sel, &press_target, now)) {
+				*changed = 1;
+				last_click_valid = 0;
+				return;
+			}
+		}
+	}
+
+	if (press_sel.kind != SRC_NONE) {
+		if (same_sel(&press_sel, &select_sel)) {
+			clear_selection();
+			last_click_valid = 0;
+		} else {
+			select_sel = press_sel;
+			last_click_sel = press_sel;
+			last_click_ms = now;
+			last_click_valid = 1;
+		}
+		*changed = 1;
+		return;
+	}
+
+	if (press_target.kind == TGT_NONE) {
+		if (select_sel.kind != SRC_NONE) {
+			clear_selection();
+			*changed = 1;
+		}
+		last_click_valid = 0;
+	}
+	(void)l;
+}
+
+static int ui_press_consumed(struct ngui *ui)
+{
+	if (help_open) {
+		struct ngui_rect box, btn;
+
+		help_geometry(ui, &box, &btn);
+		if (!point_in(&box, ui->press_x, ui->press_y)) {
+			help_open = HELP_NONE;
+			ngui_invalidate(ui);
+		}
+		return 1;
+	}
+	if (ui->press_y < MENU_H) {
+		if (menu_open != MENU_NONE &&
+		    !(ui->press_x >= 6 && ui->press_x < 50) &&
+		    !(ui->press_x >= 52 && ui->press_x < 96)) {
+			menu_open = MENU_NONE;
+			ngui_invalidate(ui);
+		}
+		return 1;
+	}
+	if (menu_open != MENU_NONE) {
+		struct ngui_rect dr;
+		int x = menu_x(menu_open);
+		int y = MENU_H;
+		int w = menu_w(menu_open);
+		int h = menu_dropdown_h(menu_open);
+
+		dr = rect_make(x, y, w, h);
+		if (!point_in(&dr, ui->press_x, ui->press_y)) {
+			menu_open = MENU_NONE;
+			ngui_invalidate(ui);
+		}
+		return 1;
+	}
+	return 0;
+}
+
+static int handle_input(struct ngui *ui, const struct sol_layout *l,
+			unsigned int now)
+{
+	int changed = 0;
+	int k;
+
+	while ((k = ngui_key(ui)) != 0) {
+		if (k == NGUI_KEY_ESC) {
+			if (help_open) {
+				help_open = HELP_NONE;
+				changed = 1;
+			} else if (menu_open != MENU_NONE) {
+				menu_open = MENU_NONE;
+				changed = 1;
+			} else {
+				quit = 1;
+			}
+		} else if (k == 'n' || k == 'N' || k == 'r' || k == 'R') {
+			new_game(now);
+			changed = 1;
+		} else if (k == 'h' || k == 'H') {
+			help_open = HELP_HOWTO;
+			menu_open = MENU_NONE;
+			changed = 1;
+		}
+	}
+
+	if (ui->pressed) {
+		if (ui_press_consumed(ui)) {
+			gesture_consumed = 1;
+			changed = 1;
+		} else {
+			game_press(ui, l, now, &changed);
+		}
+	}
+
+	if (press_active && !drag_active && !ui->released &&
+	    press_sel.kind != SRC_NONE && (ui->buttons & 1u)) {
+		int dx = ui->mouse_x - press_x;
+		int dy = ui->mouse_y - press_y;
+
+		if (dx < 0)
+			dx = -dx;
+		if (dy < 0)
+			dy = -dy;
+		if (dx > 4 || dy > 4) {
+			drag_active = 1;
+			drag_sel = press_sel;
+			drag_dx = press_x - press_card_x;
+			drag_dy = press_y - press_card_y;
+			select_sel = press_sel;
+			last_click_valid = 0;
+			changed = 1;
+		}
+	}
+
+	if (ui->released) {
+		if (gesture_consumed) {
+			gesture_consumed = 0;
+		} else if (press_active) {
+			if (drag_active) {
+				struct target t;
+
+				t = drop_target_at(l, ui->release_x,
+						   ui->release_y);
+				if (can_drop(&drag_sel, &t))
+					perform_move(&drag_sel, &t, now);
+				else
+					clear_selection();
+				drag_active = 0;
+				press_active = 0;
+				changed = 1;
+			} else {
+				select_click(l, now, &changed);
+				press_active = 0;
+			}
+		}
+	}
+	return changed;
+}
+
+/* ------------------------------------------------------------------ */
+/* win animation                                                      */
+
+static int bounce_update(struct ngui *ui, const struct sol_layout *l,
+			 unsigned int now)
+{
+	int i;
+	int changed = 0;
+
+	if (!bounce_active)
+		return 0;
+
+	if (bounce_count < BOUNCE_MAX) {
+		unsigned int wait = bounce_count == 0 ? 0u : 105u;
+
+		if ((unsigned int)(now - bounce_spawn_ms) >= wait) {
+			struct bounce_card *b = &bounce_cards[bounce_count];
+			int idx = (bounce_count * 7 + 3) % 48;
+
+			b->active = 1;
+			b->settled = 0;
+			/* A unique, non-king card per bouncing card: the four
+			 * visible foundation kings stay unique. */
+			b->id = (idx / 12) * 13 + (idx % 12);
+			b->x = l->col_x[3 + (int)(rng_next() % 4)];
+			b->y = l->top_y;
+			b->vx = (int)(rng_next() % 7) - 3;
+			if (b->vx == 0)
+				b->vx = 2;
+			b->vy = -2 - (int)(rng_next() % 4);
+			bounce_count++;
+			bounce_spawn_ms = now;
+			changed = 1;
+		}
+	}
+
+	for (i = 0; i < BOUNCE_MAX; i++) {
+		struct bounce_card *b = &bounce_cards[i];
+
+		if (!b->active)
+			continue;
+		b->x += b->vx;
+		b->y += b->vy;
+		b->vy += 1;
+
+		if (b->x < 0) {
+			b->x = 0;
+			b->vx = -b->vx;
+		}
+		if (b->x + l->cw > ui->w) {
+			b->x = ui->w - l->cw;
+			if (b->x < 0)
+				b->x = 0;
+			b->vx = -b->vx;
+		}
+		if (b->y < 0) {
+			b->y = 0;
+			if (b->vy < 0)
+				b->vy = -b->vy;
+		}
+		if (b->y + l->ch > l->status_y - 2) {
+			b->y = l->status_y - 2 - l->ch;
+			if (b->y < 0)
+				b->y = 0;
+			if (b->vy > 0)
+				b->vy = -(b->vy * 5 / 8);
+			b->vx = b->vx * 7 / 8;
+			if (b->vy > -2) {
+				b->vy = 0;
+				if (b->vx < 2 && b->vx > -2) {
+					b->vx = 0;
+					b->active = 0;
+					b->settled = 1;
+				}
+			}
+		}
+		changed = 1;
+	}
+
+	if (bounce_count >= BOUNCE_MAX) {
+		int any = 0;
+
+		for (i = 0; i < BOUNCE_MAX; i++)
+			if (bounce_cards[i].active)
+				any = 1;
+		if (!any)
+			bounce_active = 0;
+	} else {
+		changed = 1;
+	}
+
+	if ((unsigned int)(now - bounce_start_ms) > 20000u) {
+		for (i = 0; i < bounce_count; i++) {
+			bounce_cards[i].active = 0;
+			bounce_cards[i].settled = 1;
+		}
+		bounce_active = 0;
+	}
+	return changed;
+}
+
+/* ------------------------------------------------------------------ */
+/* app                                                                */
+
+static void draw_app(struct ngui *ui, const struct sol_layout *l)
+{
+	ngui_fill_rect(ui, rect_make(0, 0, ui->w, ui->h), C_FELT);
+
+	draw_stock(ui, l);
+	draw_waste(ui, l);
+	draw_foundations(ui, l);
+	draw_tableau(ui, l);
+
+	draw_drop_highlight(ui, l);
+	draw_selection_outline(ui, l);
+	draw_bounce_cards(ui, l);
+	if (drag_active)
+		draw_drag_cards(ui, l);
+
+	draw_menu_bar(ui, l);
+	draw_status(ui, l);
+	draw_dropdown(ui);
+	if (help_open)
+		draw_help(ui);
+}
+
+void _start(const char *arg)
+{
+	static const struct {
+		int w, h;
+	} sizes[] = {
+		{ START_W, START_H },
+		{ 700, 520 },
+		{ 640, 460 },
+		{ 600, 400 },
+		{ 520, 380 },
+		{ 440, 340 },
+		{ 360, 300 }
+	};
+	struct win_info info;
+	struct ngui ui;
+	unsigned int now;
+	unsigned int i;
+	int created = 0;
+
+	(void)arg;
+	for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
+		if (u_win_create((unsigned int)sizes[i].w,
+				 (unsigned int)sizes[i].h,
+				 "Solitaire", &info) == 0) {
+			created = 1;
+			break;
+		}
+	}
+	if (!created) {
+		u_puts("solitaire: no graphics window\n");
+		u_exit(1);
+	}
+
+	ngui_init(&ui, (unsigned short *)info.fb, (int)info.width,
+		  (int)info.height);
+	setup_theme(&ui);
+	draw_three = 0;
+	deck_style = 0;
+	quit = 0;
+	now = u_ticks_ms();
+	new_game(now);
+
+	for (;;) {
+		struct sol_layout l;
+		int want, changed, sec;
+
+		now = u_ticks_ms();
+		want = ngui_begin(&ui);
+		if (quit)
+			break;
+		changed = 0;
+		if (pending_action != ACT_NONE) {
+			int act = pending_action;
+
+			pending_action = ACT_NONE;
+			menu_action(act, now);
+			changed = 1;
+		}
+		compute_layout(&ui, &l);
+		changed |= handle_input(&ui, &l, now);
+		if (bounce_active)
+			changed |= bounce_update(&ui, &l, now);
+		sec = elapsed_seconds(now);
+		if (sec != last_sec) {
+			last_sec = sec;
+			changed = 1;
+		}
+		if (want || changed) {
+			draw_app(&ui, &l);
+			ngui_end(&ui);
+		} else {
+			ngui_end_ex(&ui, 0);
+		}
+		if (quit)
+			break;
+		u_sleep_ms(bounce_active ? 20 : 10);
+	}
+	u_win_close();
+	u_exit(0);
+}

+ 1 - 1
ucmd_term.c

@@ -1175,7 +1175,7 @@ static void complete_line(void)
 	} else {
 		/* Command-name completion: builtins + /ramfs/bin. */
 		static const char * const builtins[] = {
-			"cd", "path", "help", "clear", "ps", "kill"
+			"cd", "path", "help", "clear", "bootlog", "ps", "kill"
 		};
 		struct sys_dirent d;
 

+ 8 - 2
ucmd_wm.c

@@ -137,9 +137,11 @@ static void draw_bar(void)
 		   WM_BTN_MINES_H, "Mines");
 	bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
 		   WM_BTN_WEB_H, "Web");
+	bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
+		   WM_BTN_CARDS_H, "Cards");
 	if (bar_msg_until &&
 	    (int)(u_ticks_ms() - bar_msg_until) < 0)
-		usurf_text(&screen, (int)(WM_BTN_WEB_X + WM_BTN_WEB_W + 8),
+		usurf_text(&screen, (int)(WM_BTN_CARDS_X + WM_BTN_CARDS_W + 8),
 			   5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
 
 	/* Rainbow separator (same y as the kernel's old bar). */
@@ -194,7 +196,7 @@ static void draw_bar(void)
 			int tx = (int)fbw - 4 - (int)clock_chars * 8 -
 				 (clock_chars ? 8 : 0) - (int)tn * 8;
 
-			if (tx >= WM_BTN_MINES_X + (int)WM_BTN_MINES_W + 4)
+			if (tx >= WM_BTN_CARDS_X + (int)WM_BTN_CARDS_W + 4)
 				usurf_text(&screen, tx, 5, tb,
 					   temp_color(bar_temp_c), 0, 0,
 					   fbw, WM_BAR_H);
@@ -596,6 +598,10 @@ static void handle_mouse(struct wm_status *st)
 			   mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
 			   my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
 			spawn_app_checked("/ramfs/bin/browser");
+		} else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
+			   mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
+			   my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
+			spawn_app_checked("/ramfs/bin/solitaire");
 		} else {
 			int idx = window_at(mx, my);
 

+ 1 - 0
uhci.c

@@ -545,4 +545,5 @@ const struct usb_hcd_ops usb_uhci_ops = {
 	uhci_int_in_stop,
 	0,
 	0,
+	0,
 };

+ 216 - 63
usb.c

@@ -1,12 +1,22 @@
 /* USB core: device enumeration, descriptor parsing, hub recursion, bus scans
    and the hotplug watcher.  Controller-specific work goes through the
-   usb_hcd_ops vtable (uhci.c / ohci.c). */
+   usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c / xhci.c). */
 
 #include "io.h"
 #include "timer.h"
 #include "usb.h"
 #include "usb_hcd.h"
 
+/* One global controller index space.  Each entry maps to one instance of
+   one controller type; a machine with EHCI + UHCI companions gets several.
+   USB_HCD_MAX_CTRL is the per-type limit, this is the total. */
+#define USB_MAX_CTRL 8
+
+struct usb_ctrl_ref {
+	const struct usb_hcd_ops *ops;
+	unsigned int local;
+};
+
 /* Standard request types. */
 #define RT_DEV2HOST_STD 0x80u
 #define RT_HOST2DEV_STD 0x00u
@@ -20,6 +30,7 @@
 #define REQ_SET_FEATURE    0x03u
 #define REQ_SET_ADDRESS    0x05u
 #define REQ_GET_DESCRIPTOR 0x06u
+#define REQ_SET_CONFIGURATION 0x09u
 
 /* Descriptor types. */
 #define DESC_DEVICE    0x01u
@@ -32,9 +43,11 @@
 #define FEAT_PORT_RESET        4u
 #define FEAT_PORT_POWER        8u
 #define FEAT_C_PORT_CONNECTION 16u
+#define FEAT_C_PORT_RESET      20u
 
 /* Hub port status bits. */
 #define PSTAT_CONNECT   0x0001u
+#define PSTAT_ENABLE    0x0002u
 #define PSTAT_LOW_SPEED 0x0200u
 
 /* How much of a configuration descriptor we read/parse. */
@@ -85,9 +98,12 @@ static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
 }
 #endif
 
-static const struct usb_hcd_ops *hcd;
+static struct usb_ctrl_ref usb_ctrls[USB_MAX_CTRL];
+static unsigned int n_usb_ctrls;
+static int hcd_init_done;
 static unsigned int generation;
-static unsigned int next_addr[USB_HCD_MAX_CTRL];
+static unsigned int next_addr[USB_MAX_CTRL];
+static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
 static usb_probe_fn current_probe;
 static usb_device_probe_fn device_probe;
 static int print_devices;
@@ -175,10 +191,12 @@ int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
 		   unsigned short value, unsigned short index,
 		   unsigned char *buf, unsigned int len)
 {
+	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (!hcd)
+	if (ctrl >= n_usb_ctrls)
 		return -1;
+	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[1] = req;
 	setup[2] = (unsigned char)(value & 0xFF);
@@ -187,17 +205,20 @@ int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	setup[5] = (unsigned char)((index >> 8) & 0xFF);
 	setup[6] = (unsigned char)(len & 0xFF);
 	setup[7] = (unsigned char)((len >> 8) & 0xFF);
-	return hcd->control_transfer(ctrl, addr, mps, low_speed, setup, buf, len, 1);
+	return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
+				     low_speed, setup, buf, len, 1);
 }
 
 int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
 		    int low_speed, unsigned char rt, unsigned char req,
 		    unsigned short value, unsigned short index)
 {
+	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (!hcd)
+	if (ctrl >= n_usb_ctrls)
 		return -1;
+	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[1] = req;
 	setup[2] = (unsigned char)(value & 0xFF);
@@ -206,7 +227,8 @@ int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	setup[5] = (unsigned char)((index >> 8) & 0xFF);
 	setup[6] = 0;
 	setup[7] = 0;
-	return hcd->control_transfer(ctrl, addr, mps, low_speed, setup, 0, 0, 0);
+	return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
+				     low_speed, setup, 0, 0, 0);
 }
 
 int usb_control_out_data(unsigned int ctrl, unsigned int addr,
@@ -215,10 +237,12 @@ int usb_control_out_data(unsigned int ctrl, unsigned int addr,
 			 unsigned short index, const unsigned char *buf,
 			 unsigned int len)
 {
+	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (!hcd)
+	if (ctrl >= n_usb_ctrls)
 		return -1;
+	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[1] = req;
 	setup[2] = (unsigned char)(value & 0xFF);
@@ -227,8 +251,9 @@ int usb_control_out_data(unsigned int ctrl, unsigned int addr,
 	setup[5] = (unsigned char)((index >> 8) & 0xFF);
 	setup[6] = (unsigned char)(len & 0xFF);
 	setup[7] = (unsigned char)((len >> 8) & 0xFF);
-	return hcd->control_transfer(ctrl, addr, mps, low_speed, setup,
-				     (unsigned char *)buf, len, 0);
+	return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
+				     low_speed, setup, (unsigned char *)buf,
+				     len, 0);
 }
 
 /* Read the configuration descriptor and hand every interface with an
@@ -407,7 +432,21 @@ static void enumerate_device(unsigned int ctrl, unsigned int addr,
 static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
 			  int low, unsigned int port, unsigned int depth)
 {
-	unsigned int nports, p;
+	unsigned char cfg[9];
+	unsigned int nports, p, i, power_good;
+
+	/* Put the hub in its configured state before issuing hub-class
+	   requests.  The core does not configure non-HID devices itself, and
+	   some hubs (notably Intel PCH internal hubs) require this. */
+	if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
+			   REQ_GET_DESCRIPTOR,
+			   (unsigned short)(DESC_CONFIG << 8), 0, cfg,
+			   sizeof(cfg)) >= 0 && cfg[5] != 0) {
+		usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_STD,
+				REQ_SET_CONFIGURATION, (unsigned short)cfg[5],
+				0);
+		msleep(5);
+	}
 
 	if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_CLS,
 			   REQ_GET_DESCRIPTOR,
@@ -419,26 +458,65 @@ static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps
 	if (nports > 8)
 		nports = 8;
 
+	/* Power every downstream port before looking at connect status.
+	   An unpowered port reports no connection, so the old order could
+	   miss devices behind a switched hub. */
+	power_good = (unsigned int)hub_desc[5] * 2;
+	if (power_good < 20)
+		power_good = 20;
+	if (power_good > 500)
+		power_good = 500;
+	for (p = 1; p <= nports; p++)
+		usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
+				REQ_SET_FEATURE, FEAT_PORT_POWER,
+				(unsigned short)p);
+	msleep(power_good);
+
 	for (p = 1; p <= nports; p++) {
+		unsigned int st = 0;
 		int low2;
 
 		if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
 				   REQ_GET_STATUS, 0, (unsigned short)p,
 				   port_st, 4) < 0)
 			continue;
-		if (!(port_status_word() & PSTAT_CONNECT))
+		st = port_status_word();
+		if (print_devices) {
+			unsigned int chg = (unsigned int)port_st[2] |
+					   ((unsigned int)port_st[3] << 8);
+			indent(depth);
+			io_putstr("Hub port ");
+			put_uint(p);
+			io_putstr(": status 0x");
+			put_hex(st, 4);
+			io_putstr(" change 0x");
+			put_hex(chg, 4);
+			io_putchar('\n');
+		}
+		if (!(st & PSTAT_CONNECT))
 			continue;
 
-		/* Power and reset the downstream port. */
-		usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
-				REQ_SET_FEATURE, FEAT_PORT_POWER,
-				(unsigned short)p);
+		/* Reset the downstream port and wait for it to enable. */
 		if (usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
 				    REQ_SET_FEATURE, FEAT_PORT_RESET,
 				    (unsigned short)p) < 0)
 			continue;
-		msleep(30);
-
+		for (i = 0; i < 100; i++) {
+			if (usb_control_in(ctrl, addr, mps, low,
+					   RT_DEV2HOST_OTH, REQ_GET_STATUS, 0,
+					   (unsigned short)p, port_st, 4) < 0)
+				break;
+			st = port_status_word();
+			if (st & PSTAT_ENABLE)
+				break;
+			if (port_st[2] & 0x10u)   /* C_PORT_RESET */
+				break;
+			msleep(5);
+		}
+		msleep(10);
+		usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
+				REQ_CLEAR_FEATURE, FEAT_C_PORT_RESET,
+				(unsigned short)p);
 		usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
 				REQ_CLEAR_FEATURE, FEAT_C_PORT_CONNECTION,
 				(unsigned short)p);
@@ -448,9 +526,25 @@ static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps
 		if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
 				   REQ_GET_STATUS, 0, (unsigned short)p,
 				   port_st, 4) >= 0) {
-			if (port_status_word() & PSTAT_LOW_SPEED)
+			st = port_status_word();
+			if (st & PSTAT_LOW_SPEED)
 				low2 = 1;
 		}
+		if (!(st & PSTAT_ENABLE))
+			continue;
+
+		if (print_devices) {
+			unsigned int chg = (unsigned int)port_st[2] |
+					   ((unsigned int)port_st[3] << 8);
+			indent(depth);
+			io_putstr("Hub port ");
+			put_uint(p);
+			io_putstr(" after reset: status 0x");
+			put_hex(st, 4);
+			io_putstr(" change 0x");
+			put_hex(chg, 4);
+			io_putchar('\n');
+		}
 
 		enumerate_new_device(ctrl, port, low2, depth);
 	}
@@ -504,28 +598,55 @@ static void enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
 	enumerate_device(ctrl, addr, mps, low, port, depth);
 }
 
-int usb_hcd_init(void)
+static void add_ctrl(const struct usb_hcd_ops *ops, unsigned int count)
 {
-	if (hcd)
-		return 0;
-	if (usb_uhci_ops.init() > 0) {
-		hcd = &usb_uhci_ops;
-		return 0;
-	}
-	if (usb_ohci_ops.init() > 0) {
-		hcd = &usb_ohci_ops;
-		return 0;
-	}
-	if (usb_xhci_ops.init() > 0) {
-		hcd = &usb_xhci_ops;
-		return 0;
+	unsigned int i;
+
+	for (i = 0; i < count && n_usb_ctrls < USB_MAX_CTRL; i++) {
+		usb_ctrls[n_usb_ctrls].ops = ops;
+		usb_ctrls[n_usb_ctrls].local = i;
+		n_usb_ctrls++;
 	}
-	return -1;
+}
+
+int usb_hcd_init(void)
+{
+	unsigned int uhci, ohci, ehci, xhci;
+
+	if (hcd_init_done)
+		return n_usb_ctrls ? 0 : -1;
+	hcd_init_done = 1;
+
+	/* Companions first, so EHCI's CONFIGFLAG claim is the last thing
+	   that happens to the shared ports. */
+	uhci = (unsigned int)usb_uhci_ops.init();
+	ohci = (unsigned int)usb_ohci_ops.init();
+	ehci = (unsigned int)usb_ehci_ops.init();
+	xhci = (unsigned int)usb_xhci_ops.init();
+
+	/* Scan EHCI first: resetting its ports hands low/full-speed devices
+	   back to the companions before those are scanned. */
+	add_ctrl(&usb_ehci_ops, ehci);
+	add_ctrl(&usb_uhci_ops, uhci);
+	add_ctrl(&usb_ohci_ops, ohci);
+	add_ctrl(&usb_xhci_ops, xhci);
+	return n_usb_ctrls ? 0 : -1;
 }
 
 const char *usb_hcd_name(void)
 {
-	return hcd ? hcd->name : "none";
+	if (!n_usb_ctrls)
+		return "none";
+	if (n_usb_ctrls == 1)
+		return usb_ctrls[0].ops->name;
+	return "USB";
+}
+
+const char *usb_hcd_name_ctrl(unsigned int ctrl)
+{
+	if (ctrl >= n_usb_ctrls)
+		return "none";
+	return usb_ctrls[ctrl].ops->name;
 }
 
 unsigned int usb_generation(void)
@@ -535,30 +656,44 @@ unsigned int usb_generation(void)
 
 int usb_port_connected(unsigned int ctrl, unsigned int port)
 {
-	if (!hcd)
+	if (ctrl >= n_usb_ctrls)
 		return 0;
-	return hcd->port_connected(ctrl, port);
+	return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
+						   port);
 }
 
 int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
 		     unsigned int ep, unsigned int mps, int low_speed)
 {
-	if (!hcd || slot >= USB_HCD_INT_SLOTS)
+	const struct usb_hcd_ops *ops;
+	int r;
+
+	if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS)
 		return -1;
-	return hcd->int_in_start(ctrl, slot, addr, ep, mps, low_speed);
+	ops = usb_ctrls[ctrl].ops;
+	if (slot_ops[slot]) {           /* stale owner from another HCD */
+		slot_ops[slot]->int_in_stop(slot);
+		slot_ops[slot] = 0;
+	}
+	r = ops->int_in_start(usb_ctrls[ctrl].local, slot, addr, ep, mps,
+			      low_speed);
+	slot_ops[slot] = r == 0 ? ops : 0;
+	return r;
 }
 
 int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
 {
-	if (!hcd || slot >= USB_HCD_INT_SLOTS)
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
 		return 0;
-	return hcd->int_in_poll(slot, buf, len);
+	return slot_ops[slot]->int_in_poll(slot, buf, len);
 }
 
 void usb_int_in_stop(unsigned int slot)
 {
-	if (hcd && slot < USB_HCD_INT_SLOTS)
-		hcd->int_in_stop(slot);
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
+		return;
+	slot_ops[slot]->int_in_stop(slot);
+	slot_ops[slot] = 0;
 }
 
 void usb_register_device_probe(usb_device_probe_fn fn)
@@ -570,17 +705,19 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		 unsigned int mps, const unsigned char *buf,
 		 unsigned int len)
 {
-	if (!hcd || !hcd->bulk_out)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out)
 		return -1;
-	return hcd->bulk_out(ctrl, addr, ep, mps, buf, len);
+	return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
+					     mps, buf, len);
 }
 
 int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		unsigned int mps, unsigned char *buf, unsigned int len)
 {
-	if (!hcd || !hcd->bulk_in)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in)
 		return -1;
-	return hcd->bulk_in(ctrl, addr, ep, mps, buf, len);
+	return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
+					    mps, buf, len);
 }
 
 int usb_scan(usb_probe_fn probe)
@@ -597,15 +734,18 @@ int usb_scan(usb_probe_fn probe)
 	current_probe = probe;
 	print_devices = (probe == 0);
 
-	for (c = 0; c < hcd->count() && c < USB_HCD_MAX_CTRL; c++) {
+	for (c = 0; c < n_usb_ctrls; c++) {
+		const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
+		unsigned int local = usb_ctrls[c].local;
+
 		next_addr[c] = 1;
 
 		if (print_devices) {
 			unsigned int bus, dev, func, base;
 			int mmio;
 
-			hcd->info(c, &bus, &dev, &func, &base, &mmio);
-			io_putstr(hcd->name);
+			ops->info(local, &bus, &dev, &func, &base, &mmio);
+			io_putstr(ops->name);
 			io_putstr(" Host Controller (PCI ");
 			put_hex(bus, 2);
 			io_putchar(':');
@@ -615,22 +755,31 @@ int usb_scan(usb_probe_fn probe)
 			io_putstr(mmio ? ", MMIO 0x" : ", I/O 0x");
 			put_hex(base, mmio ? 8 : 4);
 			io_putstr(")\n");
+			if (ops->print_ports)
+				ops->print_ports(local);
 		}
 
-		for (p = 0; p < hcd->port_count(c); p++) {
-			unsigned int conn = (unsigned int)hcd->port_connected(c, p);
+		for (p = 0; p < ops->port_count(local); p++) {
+			unsigned int conn =
+				(unsigned int)ops->port_connected(local, p);
 
 			dbg_port(c, p, conn,
-				 (unsigned int)hcd->port_low_speed(c, p));
+				 (unsigned int)ops->port_low_speed(local, p));
 			if (!conn)
 				continue;
-			hcd->port_take_change(c, p);   /* clear stale CSC */
-			if (hcd->port_reset(c, p) != 0) {
+			ops->port_take_change(local, p);  /* clear stale CSC */
+			if (ops->port_reset(local, p) != 0) {
 				dbg("usb:   port reset failed\n");
 				continue;
 			}
 			dbg("usb:   port reset ok\n");
-			enumerate_new_device(c, p, hcd->port_low_speed(c, p), 1);
+			enumerate_new_device(c, p,
+					     ops->port_low_speed(local, p), 1);
+		}
+
+		if (print_devices && ops->print_ports) {
+			io_putstr("    After scan:\n");
+			ops->print_ports(local);
 		}
 	}
 
@@ -646,7 +795,7 @@ int usb_hotplug_poll(usb_probe_fn probe)
 	unsigned int now, c, p;
 	int n = 0;
 
-	if (!hcd || scan_busy)
+	if (!n_usb_ctrls || scan_busy)
 		return 0;
 
 	now = get_ticks_ms();
@@ -656,15 +805,19 @@ int usb_hotplug_poll(usb_probe_fn probe)
 
 	scan_busy = 1;
 	current_probe = probe;
-	for (c = 0; c < hcd->count() && c < USB_HCD_MAX_CTRL; c++) {
-		for (p = 0; p < hcd->port_count(c); p++) {
-			if (!hcd->port_take_change(c, p))
+	for (c = 0; c < n_usb_ctrls; c++) {
+		const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
+		unsigned int local = usb_ctrls[c].local;
+
+		for (p = 0; p < ops->port_count(local); p++) {
+			if (!ops->port_take_change(local, p))
 				continue;
-			if (!hcd->port_connected(c, p))
+			if (!ops->port_connected(local, p))
 				continue;
-			if (hcd->port_reset(c, p) != 0)
+			if (ops->port_reset(local, p) != 0)
 				continue;
-			enumerate_new_device(c, p, hcd->port_low_speed(c, p), 1);
+			enumerate_new_device(c, p,
+					     ops->port_low_speed(local, p), 1);
 			n++;
 		}
 	}

+ 5 - 2
usb.h

@@ -1,6 +1,7 @@
 /* USB core: device enumeration, descriptor parsing, hub recursion, bus
    scans and the hotplug watcher.  Controller-specific work (transfers, root
-   ports) goes through the usb_hcd_ops vtable (uhci.c / ohci.c). */
+   ports) goes through the usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c /
+   xhci.c). */
 #ifndef USB_H
 #define USB_H
 
@@ -9,9 +10,11 @@
 /* `lsusb` command: full scan, print the tree. */
 void usb_enumerate_and_print(void);
 
-/* Find + bring up a host controller; 0 when one is available. */
+/* Find + bring up every host controller; 0 when at least one is available. */
 int usb_hcd_init(void);
 const char *usb_hcd_name(void);
+/* Name of the controller type behind one global controller index. */
+const char *usb_hcd_name_ctrl(unsigned int ctrl);
 
 /* Full bus scan: resets every connected root port, re-enumerates and calls
    the probe for each interface (probe may be NULL to print instead).

+ 11 - 4
usb_hcd.h

@@ -1,9 +1,11 @@
 /* USB host controller driver (HCD) interface.
  *
- * usb.c drives enumeration, bus scans and the interrupt-IN consumers (USB
- * mouse + keyboard) through this vtable; uhci.c and ohci.c each implement it
- * for their controller type.  One "controller instance" is one PCI function
- * (MCP79 exposes four OHCI ones, for example). */
+ * usb.c drives enumeration, bus scans and the transfer consumers (USB mouse
+ * + keyboard, USB Ethernet) through this vtable; uhci.c/ohci.c/ehci.c/xhci.c
+ * each implement it for their controller type.  One "controller instance" is
+ * one PCI function (MCP79 exposes four OHCI ones, for example).  A machine
+ * with EHCI and UHCI/OHCI companions drives all of them: high-speed devices
+ * enumerate on EHCI, the rest on the companions. */
 #ifndef USB_HCD_H
 #define USB_HCD_H
 
@@ -87,10 +89,15 @@ struct usb_hcd_ops {
 	int (*bulk_in)(unsigned int ctrl, unsigned int addr,
 		       unsigned int ep, unsigned int mps,
 		       unsigned char *buf, unsigned int len);
+
+	/* Optional: print raw root-port status during `lsusb` (print mode
+	 * only).  Used by EHCI to show PORTSC while debugging routing. */
+	void (*print_ports)(unsigned int ctrl);
 };
 
 extern const struct usb_hcd_ops usb_uhci_ops;
 extern const struct usb_hcd_ops usb_ohci_ops;
+extern const struct usb_hcd_ops usb_ehci_ops;
 extern const struct usb_hcd_ops usb_xhci_ops;
 
 #endif

+ 1 - 1
usbhid.c

@@ -275,7 +275,7 @@ static void dev_attach(struct hid_dev *d, unsigned int slot)
 	serial_puts(SERIAL_COM1, d->kind == KIND_MOUSE
 				   ? "usbhid: mouse attached ("
 				   : "usbhid: keyboard attached (");
-	serial_puts(SERIAL_COM1, usb_hcd_name());
+	serial_puts(SERIAL_COM1, usb_hcd_name_ctrl(d->ctrl));
 	serial_puts(SERIAL_COM1, ", addr ");
 	serial_putdec(SERIAL_COM1, d->addr);
 	serial_puts(SERIAL_COM1, " ep ");

+ 32 - 2
vesa_stage.S

@@ -6,8 +6,10 @@
  * low gap at 0x60000..0x9FBFF (16 KB staging chunks at 0x3000, BIOS disk
  * reads only), walks the VBE mode list and picks the best mode (highest
  * resolution, >= 16 bpp, LFB, fits in VRAM), writes the mode info to 0x600
- * and the boot info to 0x500, then enables A20, enters protected mode and
- * jumps to kernel_entry.
+ * and the boot info to 0x500, then enables A20, enters protected mode,
+ * copies the pack above the 8 MB VGA-shadow reserve (PACK_HIGH_ADDR) and
+ * jumps to kernel_entry.  Relocating the pack before the kernel runs lets
+ * the kernel's BSS/boot stack extend past 0x60000.
  *
  * If no VESA mode works, boots in text mode (valid=0 at 0x600).
  */
@@ -24,6 +26,7 @@
 .equ ROOTFS_VERSION,   1
 .equ PACK_LOW_ADDR,    0x60000       /* low gap behind the kernel */
 .equ PACK_LOW_MAX,     0x3FBFF       /* 0x60000..0x9FBFF */
+.equ PACK_HIGH_ADDR,   0x900000      /* above the 8 MB VGA-shadow reserve */
 .equ BOOT_INFO_MAGIC,  0x4E425349
 .equ BOOT_INFO_VERSION, 2
 .equ BOOT_INFO_WORDS,  12
@@ -562,6 +565,33 @@ stage2_pm:
 	movw  %ax, %gs
 	movw  %ax, %ss
 	movl  $__boot_stack_top, %esp
+
+	/* Move the pack out of the low gap before the kernel clears its BSS
+	 * or uses the boot stack (both may now live above 0x60000). */
+	cld
+	movl  pack_addr, %esi
+	testl %esi, %esi
+	jz    2f
+	movl  pack_bytes, %ecx
+	testl %ecx, %ecx
+	jz    2f
+	movl  $PACK_HIGH_ADDR, %edi
+	rep   movsb
+	movl  $PACK_HIGH_ADDR, pack_addr
+
+	/* Rewrite boot info: zero checksum, new pack address, recompute the
+	 * twelve-word sum (must match bootinfo.h / write_bootinfo). */
+	movl  $0, 0x508
+	movl  $PACK_HIGH_ADDR, 0x514
+	xorl  %eax, %eax
+	movl  $0x500, %esi
+	movl  $BOOT_INFO_WORDS, %ecx
+1:	addl  (%esi), %eax
+	addl  $4, %esi
+	loop  1b
+	negl  %eax
+	movl  %eax, 0x508
+2:
 	jmp   kernel_entry
 
 .align 4

+ 3 - 3
vfs.c

@@ -244,10 +244,10 @@ static int fat_op_rmdir(struct vfs_mount *m, const char *path)
 }
 
 /* Index-based listing used to re-walk the FAT directory for every index, and
- * the WebDAV server asks for 0..N-1 in one PROPFIND.  Cache the first 64
+ * the WebDAV server asks for 0..N-1 in one PROPFIND.  Cache the first 24
  * entries of the last directory so a listing is one directory walk; a write
- * invalidates it.  Larger directories fall back to the old walk past 64. */
-#define FAT_LIST_CACHE 32
+ * invalidates it.  Larger directories fall back to the old walk past 24. */
+#define FAT_LIST_CACHE 24
 
 static struct vfs_dirent fat_cache[FAT_LIST_CACHE];
 static unsigned int fat_cache_count;

+ 4 - 0
wm_abi.h

@@ -34,6 +34,10 @@
 #define WM_BTN_WEB_Y WM_BTN_Y
 #define WM_BTN_WEB_W 56
 #define WM_BTN_WEB_H WM_BTN_H
+#define WM_BTN_CARDS_X (WM_BTN_WEB_X + WM_BTN_WEB_W + WM_BTN_GAP)
+#define WM_BTN_CARDS_Y WM_BTN_Y
+#define WM_BTN_CARDS_W 56
+#define WM_BTN_CARDS_H WM_BTN_H
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
 

+ 1 - 0
xhci.c

@@ -1491,4 +1491,5 @@ const struct usb_hcd_ops usb_xhci_ops = {
 	stop_int,
 	xhci_bulk_out,
 	xhci_bulk_in,
+	0,
 };

Некоторые файлы не были показаны из-за большого количества измененных файлов