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
- Variables
- func Allow(e AllowEntry) error
- func ClassName(c uint8) string
- func DetachPort(n int) error
- func Disallow(busid string) (bool, error)
- func ExportedBusIDs() []string
- func IsAllowed(busid, vid, pid string) bool
- func ReadOpHeader(r io.Reader) (code uint16, status uint32, err error)
- func ReadReqImport(r io.Reader) (string, error)
- func Release(busid string) error
- func StatusText(st uint32) string
- func ValidBusID(s string) bool
- func WriteRepDevlist(w io.Writer, entries []Entry) error
- func WriteRepImport(w io.Writer, status uint32, dev *Device) error
- func WriteReqDevlist(w io.Writer) error
- func WriteReqImport(w io.Writer, busid string) error
- type AllowEntry
- type Attached
- type AttachedPort
- type Attachment
- type Device
- type DeviceInfo
- type Entry
- type HostError
- type Interface
- type VhciPort
Constants ¶
const ( Version = 0x0111 OpReqDevlist uint16 = 0x8005 OpRepDevlist uint16 = 0x0005 OpReqImport uint16 = 0x8003 OpRepImport uint16 = 0x0003 )
USB/IP protocol constants (Documentation/usb/usbip_protocol.rst).
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.
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).
const Service = "usb"
Service is the wire service name.
Variables ¶
var ErrNoDevice = errors.New("no such USB device")
ErrNoDevice means the busid is not a device of this machine.
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 DetachPort ¶
DetachPort detaches vhci port index n (as shown by `remohop usb`).
func ExportedBusIDs ¶
func ExportedBusIDs() []string
ExportedBusIDs lists the devices currently bound to usbip-host.
func ReadOpHeader ¶
ReadOpHeader reads and checks an OP_ header.
func ReadReqImport ¶
ReadReqImport reads the body of OP_REQ_IMPORT (after its header) and returns the busid, which is validated.
func Release ¶
Release returns a device stuck on usbip-host (after a crash) to its normal driver. It is the `remohop usb release` command.
func ValidBusID ¶
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 ¶
WriteRepDevlist sends OP_REP_DEVLIST.
func WriteRepImport ¶
WriteRepImport sends OP_REP_IMPORT; dev is sent only with StOK.
func WriteReqDevlist ¶
WriteReqDevlist sends OP_REQ_DEVLIST.
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 ¶
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.
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 ReadRepImport ¶
ReadRepImport reads the body of OP_REP_IMPORT for a header with status st.
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.
type Entry ¶
Entry is a device of an OP_REP_DEVLIST with its interfaces.
type HostError ¶
type HostError struct{ Msg string }
HostError is a refusal that came from the host, with the host's reason.
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.