usb

package
v1.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package usb exports USB devices of the host to the client over the session using the Linux USB/IP kernel drivers (Linux on both sides, root on both sides).

The host binds an allowed device to the usbip-host driver; the client imports it through vhci_hcd. Both kernel drivers speak the standard USB/IP protocol on a socket, so each side makes a loopback TCP connection, hands one end to the kernel (usbip_sockfd on the host, vhci_hcd attach on the client) and relays the other end over a session stream.

Wire protocol, service "usb":

  • request {op:"list"}: the host answers one JSON {ok, error, note, devices:[...]} (a rich listing with strings and an allowed flag) and ends the stream.
  • request {op:"usbip"}: the stream then carries the standard USB/IP protocol (version 0x0111): OP_REQ_DEVLIST gets OP_REP_DEVLIST (the devices the host owner allowed); OP_REQ_IMPORT with a busid gets OP_REP_IMPORT, and after a successful import the stream carries the raw URB traffic of the two kernels until either side closes it. An extension: after a refusing OP_REP_IMPORT status the host appends one JSON message {error} explaining why (a standard client ignores it).

Exporting detaches a device from its host driver, so a device is exported only if the host owner allowed that busid and VID:PID with `remohop usb allow`, and the original driver is rebound when the client detaches or the session ends.

Index

Constants

View Source
const (
	Version = 0x0111

	OpReqDevlist uint16 = 0x8005
	OpRepDevlist uint16 = 0x0005
	OpReqImport  uint16 = 0x8003
	OpRepImport  uint16 = 0x0003
)

USB/IP protocol constants (Documentation/usb/usbip_protocol.rst).

View Source
const (
	StOK      uint32 = 0
	StNA      uint32 = 1 // request not possible
	StDevBusy uint32 = 2
	StDevErr  uint32 = 3
	StNoDev   uint32 = 4
	StError   uint32 = 5
)

Status codes of an OP_REP header.

View Source
const (
	SpeedUnknown   = 0
	SpeedLow       = 1
	SpeedFull      = 2
	SpeedHigh      = 3
	SpeedWireless  = 4
	SpeedSuper     = 5
	SpeedSuperPlus = 6
)

USB device speeds as the kernel numbers them (enum usb_device_speed).

View Source
const Service = "usb"

Service is the wire service name.

Variables

View Source
var ErrNoDevice = errors.New("no such USB device")

ErrNoDevice means the busid is not a device of this machine.

View Source
var ErrNoVHCI = errors.New("vhci_hcd is not loaded")

ErrNoVHCI means vhci_hcd is not loaded.

Functions

func Allow

func Allow(e AllowEntry) error

Allow records e (replacing any entry for the same busid).

func ClassName

func ClassName(c uint8) string

ClassName names a USB class code.

func DetachPort

func DetachPort(n int) error

DetachPort detaches vhci port index n (as shown by `remohop usb`).

func Disallow

func Disallow(busid string) (bool, error)

Disallow removes busid; it reports whether it was allowed.

func ExportedBusIDs

func ExportedBusIDs() []string

ExportedBusIDs lists the devices currently bound to usbip-host.

func IsAllowed

func IsAllowed(busid, vid, pid string) bool

IsAllowed reports whether device busid with this VID:PID may be exported.

func ReadOpHeader

func ReadOpHeader(r io.Reader) (code uint16, status uint32, err error)

ReadOpHeader reads and checks an OP_ header.

func ReadReqImport

func ReadReqImport(r io.Reader) (string, error)

ReadReqImport reads the body of OP_REQ_IMPORT (after its header) and returns the busid, which is validated.

func Release

func Release(busid string) error

Release returns a device stuck on usbip-host (after a crash) to its normal driver. It is the `remohop usb release` command.

func StatusText

func StatusText(st uint32) string

StatusText describes a non-zero OP_REP status.

func ValidBusID

func ValidBusID(s string) bool

ValidBusID reports whether s is a USB bus id like "1-2" or "3-1.4.2". The strict form matters: busids are used to build sysfs paths.

func WriteRepDevlist

func WriteRepDevlist(w io.Writer, entries []Entry) error

WriteRepDevlist sends OP_REP_DEVLIST.

func WriteRepImport

func WriteRepImport(w io.Writer, status uint32, dev *Device) error

WriteRepImport sends OP_REP_IMPORT; dev is sent only with StOK.

func WriteReqDevlist

func WriteReqDevlist(w io.Writer) error

WriteReqDevlist sends OP_REQ_DEVLIST.

func WriteReqImport

func WriteReqImport(w io.Writer, busid string) error

WriteReqImport sends OP_REQ_IMPORT for busid.

Types

type AllowEntry

type AllowEntry struct {
	BusID string `json:"busid"`
	VID   string `json:"vid"`
	PID   string `json:"pid"`
}

AllowEntry is one device the host owner agreed to export. The VID:PID is recorded with the busid so that a different device plugged into the same port later is not exported by accident.

func Allowed

func Allowed() ([]AllowEntry, error)

Allowed lists the devices the host owner allowed.

type Attached

type Attached struct {
	BusID  string
	Device Device
	// Port is the kernel's vhci port number (what `remohop usb detach N`
	// takes).
	Port int
	// contains filtered or unexported fields
}

Attached is a remote device imported through vhci_hcd.

func Attach

func Attach(ctx context.Context, c *peer.Conn, busid string) (*Attached, error)

Attach imports the host's device busid: it asks the host to export it, hands the kernel a loopback socket and relays it over the session.

func (*Attached) Close

func (a *Attached) Close() error

Close detaches the device and ends the relay.

func (*Attached) Done

func (a *Attached) Done() <-chan struct{}

Done is closed when the device is detached or the session ends.

type AttachedPort

type AttachedPort struct {
	Port       int    `json:"port"`
	LocalBusID string `json:"local_busid"`
	DevID      uint32 `json:"devid"`
	Speed      int    `json:"speed"`
}

AttachedPort is a device imported on this machine.

func ListAttached

func ListAttached() ([]AttachedPort, error)

ListAttached lists the vhci ports in use on this machine.

type Attachment

type Attachment struct {
	Port       int    `json:"port"`
	LocalBusID string `json:"local_busid"`
	BusID      string `json:"busid,omitempty"` // the host's busid
	Target     string `json:"target,omitempty"`
	PID        int    `json:"pid,omitempty"`
	Device     string `json:"device,omitempty"`
}

Attachment is an imported device with its owning remohop process, if any.

type Device

type Device struct {
	Path          string
	BusID         string
	BusNum        uint32
	DevNum        uint32
	Speed         uint32
	VendorID      uint16
	ProductID     uint16
	BCDDevice     uint16
	Class         uint8
	SubClass      uint8
	Protocol      uint8
	ConfigValue   uint8
	NumConfigs    uint8
	NumInterfaces uint8
}

Device is struct usbip_usb_device.

func ParseDevice

func ParseDevice(b []byte) (Device, error)

ParseDevice decodes the wire struct.

func ReadRepImport

func ReadRepImport(r io.Reader, st uint32) (*Device, error)

ReadRepImport reads the body of OP_REP_IMPORT for a header with status st.

func (Device) DevID

func (d Device) DevID() uint32

DevID is the id the vhci driver wants: bus number and device number.

func (Device) Marshal

func (d Device) Marshal() []byte

Marshal encodes the device as the 312-byte wire struct.

type DeviceInfo

type DeviceInfo struct {
	BusID        string   `json:"busid"`
	VID          string   `json:"vid"`
	PID          string   `json:"pid"`
	Manufacturer string   `json:"manufacturer,omitempty"`
	Product      string   `json:"product,omitempty"`
	Serial       string   `json:"serial,omitempty"`
	Class        string   `json:"class"`                // device class, or the interfaces' classes if the device says "per interface"
	Interfaces   []string `json:"interfaces,omitempty"` // interface class names
	Speed        string   `json:"speed,omitempty"`      // Mbit/s as the kernel writes it ("480", "5000")
	Driver       string   `json:"driver,omitempty"`     // current kernel driver of the device
	Allowed      bool     `json:"allowed"`              // the host owner allowed exporting it
	Exported     bool     `json:"exported"`             // bound to usbip-host
	InUse        bool     `json:"in_use"`               // a client is using it right now
}

DeviceInfo is what `usb list` shows about a host device. Strings are peer-supplied on the client: print them through cli.Sanitize.

func List

func List(ctx context.Context, c *peer.Conn) (devs []DeviceInfo, note string, err error)

List asks the host for its USB devices. note, when non-empty, says why the host cannot export yet (a missing module). Strings are bounded but must be sanitised before printing.

type Entry

type Entry struct {
	Device     Device
	Interfaces []Interface
}

Entry is a device of an OP_REP_DEVLIST with its interfaces.

func DevList

func DevList(ctx context.Context, c *peer.Conn) ([]Entry, error)

DevList asks the host, with the standard OP_REQ_DEVLIST, for the devices its owner allowed to export.

func ReadRepDevlist

func ReadRepDevlist(r io.Reader) ([]Entry, error)

ReadRepDevlist reads the body of OP_REP_DEVLIST (after its header), bounding the device count.

type HostError

type HostError struct{ Msg string }

HostError is a refusal that came from the host, with the host's reason.

func (*HostError) Error

func (e *HostError) Error() string

type Interface

type Interface struct{ Class, SubClass, Protocol uint8 }

Interface is struct usbip_usb_interface (the 4th byte is padding).

type VhciPort

type VhciPort struct {
	Controller int    `json:"controller"`
	Hub        string `json:"hub"`    // "hs" or "ss"
	Port       int    `json:"-"`      // the kernel's port number (global over controllers)
	Index      int    `json:"port"`   // what `usb detach N` takes: the same number
	Status     int    `json:"status"` // 4 free, 6 in use
	Speed      int    `json:"speed"`
	DevID      uint32 `json:"devid"`
	LocalBusID string `json:"local_busid"`
}

VhciPort is one virtual USB port of vhci_hcd.

The kernel numbers ports across all controllers (port = controller * ports-per-controller + root-hub port, the high-speed hub's ports before the super-speed hub's), prints that number in the status files and takes it in attach and detach, which only exist on vhci_hcd.0.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL