Files
2026-11-25 05:09:31 -05:00

141 lines
6.9 KiB
Markdown

# attach/linux/ — Linux USB attach via usbip/vhci (todo 9)
Presents the `core/protocol` emitter (`nui::protocol::KinectV1Device`, a
`UsbDeviceBackend`) as a Linux USB/IP-exportable virtual Kinect v1: a
userspace USB/IP **server** speaking the Kinect's three sibling-device
descriptors (camera `1-1`, audio `1-2`, motor `1-3` — see
`core/protocol/wire.h`) plus control transfers and iso-style stream packets,
attachable through the stock `vhci-hcd` kernel driver + `usbip` client CLI.
No out-of-tree kernel modules.
## Status in THIS environment: runtime attach is **[~] BLOCKED**
Verified on this box (2026-11-25, same findings as `docs/spike-linux.md`
todo 3):
| Check | Result |
| --- | --- |
| `uname -r` | `6.6.87.2-microsoft-standard-WSL2` |
| `modprobe -n -v vhci-hcd` | resolves the full chain (`usb-common` → `usbcore` → `usbip-core` → `vhci-hcd`), so the module **exists** in this kernel |
| `lsmod \| grep vhci` | **not loaded** (exit 1) |
| `/sys/devices/platform/vhci_hcd*` | **does not exist** (module never loaded → no vhci sysfs attach points) |
| `command -v usbip` | **absent** (exit 1) — no usbip userspace client is installed |
| `sudo -n true` | **requires interactive auth** — no non-interactive sudo available to this session |
**Bounded no-sudo installability check** (this task's own probe, run before
writing this doc): `apt-get download usbip` → `E: Unable to locate package
usbip` (no such package name on this Ubuntu release). `apt-get download
linux-tools-generic` succeeds (no sudo needed — `apt-get download` writes to
the cwd), but extracting it (`dpkg-deb -x`) shows it is an **empty
meta-package**: its only payload is `usr/share/doc/...` (changelog +
copyright), zero binaries. The real `usbip` binary ships inside a
kernel-version-specific package (`linux-tools-<exact-uname-r>` /
`linux-tools-<flavour>`), which does not exist for a custom WSL2 kernel
string (`6.6.87.2-microsoft-standard-WSL2` is not an Ubuntu package suffix).
**Conclusion: usbip userspace is not installable here even with sudo**,
because no matching `linux-tools-*` package is published for this kernel;
the no-sudo path was checked first and independently fails for the same
underlying reason (no matching package, not merely a permissions problem).
Runtime attach (`lsusb` showing `045e:02ae`/`02ad`/`02b0`, or
`freenect-glview`/an OpenNI viewer opening the device) **cannot be exercised
in this environment**. The code in this directory is complete and compiles;
only the live USB-level verification is blocked.
## What is implemented
- `include/attach_linux/usbip_protocol.h` + `src/usbip_protocol.cpp` — USB/IP
wire protocol encode/decode (`OP_REQ_DEVLIST`/`OP_REP_DEVLIST`,
`OP_REQ_IMPORT`/`OP_REP_IMPORT`, `USBIP_CMD_SUBMIT`/`USBIP_RET_SUBMIT`,
`USBIP_CMD_UNLINK`/`USBIP_RET_UNLINK`), transcribed from
<https://www.kernel.org/doc/html/latest/usb/usbip_protocol.html>
(retrieved 2026-11-25; protocol v1.1.1 / wire value `0x0111`). No sockets —
unit-tested as pure byte-buffer transforms.
- `include/attach_linux/usbip_server.h` + `src/usbip_server.cpp` — a
POSIX-socket `UsbipServer` that answers the device-list/import handshake
and then relays `USBIP_CMD_SUBMIT`/`USBIP_CMD_UNLINK` traffic into a
`nui::protocol::UsbDeviceBackend&`, one control + bulk/iso-style session
per connection.
- `include/attach_linux/kinect_export.h` + `src/kinect_export.cpp` — the
three exported-device descriptors (camera/audio/motor busids matching
`core/protocol/wire.h`'s VID/PID/bcdDevice) and a placeholder single-frame
`FrameSource` for standalone CLI testing (real frame data comes from the
tracker/synth pipeline at a higher integration layer — out of this todo's
scope).
- `include/attach_linux/vhci_lifecycle.h` + `src/vhci_lifecycle.cpp` —
thin wrappers around the stock `usbip` **client** CLI (`usbip attach`,
`usbip detach`, `usbip port`) and `modprobe vhci-hcd`; this shim is the
server half only, per todo 9's "no out-of-tree kernel modules beyond stock
usbip/vhci".
- `src/main.cpp` — the `kinect_attach` CLI: `serve`, `attach`, `detach`,
`list`, `--help`.
- `tests/test_usbip_protocol.cpp` — assert-based round-trip tests for every
encode/decode pair above, run via `ctest`.
## Build (achievable here)
```sh
cmake -S attach/linux -B attach/linux/build
cmake --build attach/linux/build
ctest --test-dir attach/linux/build
```
This is a standalone CMake project: it does **not** touch the root
`CMakeLists.txt` or `build/linux`. It pulls in `core/`'s `libnui_core` via
its own `add_subdirectory(../../core)` with `NUI_CORE_BUILD_TESTS=OFF`, so
configuring here never fetches googletest.
## Prerequisites to actually attach (once available)
1. **Install the usbip userspace** (needs a kernel whose `linux-tools-*`
package exists, or a from-source build of `tools/usb/usbip` against this
kernel's headers):
```sh
sudo apt-get install -y usbip # Ubuntu/Debian, if the package exists for your kernel
# or, if not packaged for your exact kernel string (WSL2's usual case):
# build tools/usb/usbip from a matching Linux kernel source tree
```
2. **Load vhci-hcd** (needs root):
```sh
sudo modprobe vhci-hcd
ls /sys/devices/platform/vhci_hcd.0 # must now exist
```
**WSL2 caveat**: even after a successful `modprobe`, WSL2's init can lack
the udev/sysfs plumbing `usbip` expects for hot-plug notification; if
`usbip attach` hangs or the imported device never appears under `lsusb`,
that plumbing gap — not this shim — is the next thing to debug. A
native-Linux host sidesteps it entirely.
## Run / attach / detach (once prerequisites are met)
```sh
# Terminal 1: start the server (defaults to port 3240, the usbip client's own default).
./attach/linux/build/kinect_attach serve --port 3240
# Terminal 2: attach the camera device through vhci-hcd.
sudo modprobe vhci-hcd
./attach/linux/build/kinect_attach attach --host 127.0.0.1 --port 3240 --busid 1-1
lsusb | grep 045e # expect 045e:02ae
# ... use the device (freenect-glview / an OpenNI2 viewer against it) ...
# Find the vhci port it landed on, then detach.
./attach/linux/build/kinect_attach list
./attach/linux/build/kinect_attach detach --vhci-port <N>
```
Repeat `attach` with `--busid 1-2` / `--busid 1-3` to also attach the audio
and motor sibling devices.
## Known simplifications (documented, not hidden)
- The placeholder `FrameSource` in `kinect_export.cpp` always returns one
all-background frame; it is for compiling/smoke-testing this shim only,
not a substitute for the tracker/synth pipeline (todos 5/7/10).
- Each `USBIP_CMD_SUBMIT` against a camera iso endpoint is answered with one
`UsbDeviceBackend::next_packet()` call (one USB/IP-tunnelled packet per
request), with no ISO packet descriptor array modelled — correct for this
server's read-only (IN) iso endpoints, where the client never sends ISO
descriptors on the wire it is requesting from (the OUT-direction case,
which never applies to these endpoints, is intentionally unhandled).