Model USB device mode, with an emulated PC on the cable

Both USB controllers were plain register caches: USBCMD.RESET never
cleared, so bdk's usb_device_init() timed out and none of Nyx's USB
tools could even wait for a cable.

- The Erista USB2 controller (USB1, 0x7D000000, bdk usbd.c): queue
  heads at ENDPOINTLISTADDR (bdk's are in the controller at +0x1000),
  transfer descriptors in DRAM, ENDPTPRIME / ENDPTSTATUS /
  ENDPTCOMPLETE, SETUP packets in queue head 0 with ENDPTSETUPSTAT,
  a self-clearing controller reset and the PHY's clock-valid bit. Byte
  and halfword accesses work, since bdk memcpy()s SETUP packets out.
- The Mariko XUSB device controller (0x700D0000, bdk xusbd.c): endpoint
  contexts and TRB rings with Link TRBs and cycle bits, the doorbell, a
  two-segment event ring with Port Status Change, Setup and Transfer
  events, EP_HALT / EP_STCHG, EP_RELOAD, and PORTSC's write-1-to-clear
  changes and link-state writes.
- The host (--usb-host, [usb] host, or the config window) plays the PC
  on whichever controller attaches: debounce, bus reset at high speed,
  enumeration with SET_ADDRESS recovery, then after 100 ms for its class
  driver to bind - bdk's XUSB driver stalls an early GET_MAX_LUN - the
  class. For mass storage it runs Bulk-Only Transport: GET_MAX_LUN,
  INQUIRY, TEST UNIT READY with REQUEST SENSE for the unit attention,
  READ CAPACITY, READ(10) of the first 64 and last 8 blocks with their
  CRC32s, checked against the SD image when the size matches, then allow
  removal, eject, and poll until the gadget detaches. For HID it reads
  the report descriptor, sets idle 0 and polls the interrupt endpoint.
  It only reads, and it runs on the controllers' register accesses, so
  runs repeat. [usb-host] lines log it; RCM_USB_TRACE=1 logs every
  descriptor, TRB and doorbell.

With hekate 6.5.3, Nyx's SD card mass storage enumerates as 11EC:A7E0,
reads back identical to the SD image and ends at "Disk ejected" on both
Erista and Mariko; the gamepad enumerates and serves its report
descriptor (with no Joy-Cons it has nothing to report). Without a host
Nyx waits and reports a timeout, as with no cable.

tests/usb/ runs a mass storage gadget over a RAM disk on each controller
and a HID gadget on the USB2 one, checks what the host logs - the
enumeration, the SCSI commands, CRC32s equal to the payload's, the eject
and the detach - and a run with nothing on the cable.

The Windows pread/pwrite shim moves from mmio.cpp to platform.h, as the
host reads the SD image too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
Claude committed 2026-09-26 10:25:29 +00:00
1 parent 66600dafc1
commit d661f2738c
16 files changed
+2342 -34

No files matched your search

+10
View File
@@ -193,6 +193,16 @@ constexpr uint64_t SE_SIZE = 0x2000;
constexpr uint64_t MSELECT_BASE = 0x50060000;
constexpr uint64_t MSELECT_SIZE = 0x1000;
// USB2 controller USB1 ("OTG"): the device controller bdk's usbd.c drives on
// Erista. Registers, the UTMI PHY at +0x400/+0x800, queue heads at +0x1000.
constexpr uint64_t USB_OTG_BASE = 0x7D000000;
constexpr uint64_t USB_OTG_SIZE = 0x4000;
// XUSB device controller: bdk's xusbd.c on Mariko. XHCI registers, then its
// PCI configuration space at +0x8000 and device registers at +0x9000.
constexpr uint64_t XUSB_DEV_BASE = 0x700D0000;
constexpr uint64_t XUSB_DEV_SIZE = 0xA000;
// XUSB pad controller. Owns the UPHY SERDES lanes shared by PCIe (pcie-0..4)
// and USB 3.0 SuperSpeed (pcie-5/6 on this board), plus UPHY PLL P0.
constexpr uint64_t XUSB_PADCTL_BASE = 0x7009F000;