Files
sing-box/service/usbip/urb_engine.go
T
世界 6a4ae5263e usbip: extract URBEngine + userspaceURBSession from darwin backend
Both the existing darwin backend and the upcoming windows backend
drive USB devices from user space (IOUSBHost CGO calls vs. VBoxUSB
IOCTLs). Refactor the per-attachment URB loop out of host_darwin.go
into a platform-agnostic userspaceURBSession that talks to a URBEngine
interface; the darwin-specific dispatch becomes a 30-line
darwinIOUSBHostEngine. Linux's kernelHandoffSession is untouched.

Move hex8 into shared.go and add usbipStatusEIO so the shared session
does not depend on golang.org/x/sys/unix (Windows has no equivalent).
2026-06-09 10:42:35 +08:00

50 lines
2.0 KiB
Go

//go:build linux || (darwin && cgo)
package usbip
// URBEngine executes USB Request Blocks against an already-claimed
// device. The session layer (session_userspace.go) handles framing,
// per-endpoint ordering, and unlink bookkeeping; the engine performs
// the per-URB I/O and per-endpoint aborts only.
//
// Submit is called from per-endpoint goroutines; the session never
// issues two Submits concurrently for the same endpoint, so the engine
// does not need its own cross-endpoint serialization.
type URBEngine interface {
Submit(request URBRequest) URBResponse
// AbortEndpoint cancels all in-flight submits on the given raw
// endpoint address (direction bit included). It is invoked once per
// pending sequence at CMD_UNLINK time and once per active endpoint
// at session shutdown.
AbortEndpoint(endpoint uint8) error
// Close releases engine-owned resources. For engines that own the
// underlying device handle (e.g. Windows VBoxUSB), this releases it.
// For engines where the host manages the device handle separately
// (e.g. Darwin IOUSBHost capture), Close may be a no-op. Idempotent.
Close() error
}
// URBRequest carries one decoded CMD_SUBMIT plus session-owned buffers.
// Buffer holds the OUT payload on entry, or a pre-allocated zero buffer
// for IN transfers. IsoPackets is pre-cloned from the wire command so
// the engine may overwrite descriptors in place during iso completion.
type URBRequest struct {
Command SubmitCommand
Endpoint uint8
Buffer []byte
IsoPackets []IsoPacketDescriptor
}
// URBResponse is the engine's verdict on one URB. Status follows USBIP
// convention (negated errno, 0 on success). ActualLength is the number
// of payload bytes valid in Buffer. Error is engine-internal failure
// distinct from a USB-level error: on Error the session emits
// Status = usbipStatusEIO and logs at Debug.
type URBResponse struct {
Status int32
ActualLength int32
Buffer []byte
IsoPackets []IsoPacketDescriptor
Error error
}