From a8f5af7bd0c04dac9d0a624312f5b2b31f9fdffc Mon Sep 17 00:00:00 2001 From: AlexSSD7 Date: Sun, 27 Aug 2023 18:07:51 +0100 Subject: [PATCH] Use USB vendor/product IDs instead of bus/dev IDs --- cmd/ls.go | 12 ++++++------ utils/utils.go | 7 +++++++ vm/types.go | 4 ++-- vm/vm.go | 15 ++++++++------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index 0edba4c..8005d29 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -46,21 +46,21 @@ func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig { os.Exit(1) } - usbBus, err := strconv.ParseUint(usbValsSplit[0], 10, 8) + vendorID, err := strconv.ParseUint(usbValsSplit[0], 16, 32) if err != nil { - slog.Error("Bad USB device bus number", "value", usbValsSplit[0]) + slog.Error("Bad USB vendor ID", "value", usbValsSplit[0]) os.Exit(1) } - usbPort, err := strconv.ParseUint(usbValsSplit[1], 10, 8) + productID, err := strconv.ParseUint(usbValsSplit[1], 16, 32) if err != nil { - slog.Error("Bad USB device port number", "value", usbValsSplit[1]) + slog.Error("Bad USB product ID", "value", usbValsSplit[1]) os.Exit(1) } return vm.USBDevicePassthroughConfig{ - HostBus: uint8(usbBus), - HostPort: uint8(usbPort), + VendorID: uint16(vendorID), + ProductID: uint16(productID), } default: slog.Error("Unknown device passthrough type", "value", valSplit[0]) diff --git a/utils/utils.go b/utils/utils.go index a4f4458..0bc71cd 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "encoding/binary" "regexp" "strings" "unicode" @@ -28,3 +29,9 @@ func ValidateDevName(s string) bool { return devNameRegexp.MatchString(s) } + +func Uint16ToBytesBE(v uint16) []byte { + b := make([]byte, 2) + binary.BigEndian.PutUint16(b, v) + return b +} diff --git a/vm/types.go b/vm/types.go index 9896fcd..c844f87 100644 --- a/vm/types.go +++ b/vm/types.go @@ -10,8 +10,8 @@ import ( ) type USBDevicePassthroughConfig struct { - HostBus uint8 - HostPort uint8 + VendorID uint16 + ProductID uint16 } type PortForwardingRule struct { diff --git a/vm/vm.go b/vm/vm.go index 401eaa4..c8a8f85 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -4,13 +4,13 @@ import ( "bufio" "bytes" "context" + "encoding/hex" "fmt" "io" "os" "os/exec" "path/filepath" "runtime" - "strconv" "sync" "sync/atomic" "syscall" @@ -122,17 +122,18 @@ func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) { if !cfg.ShowDisplay { cmdArgs = append(cmdArgs, "-display", "none") - if runtime.GOARCH == "arm64" { - // No video is configured by default in ARM. This will enable it. - cmdArgs = append(cmdArgs, "-device", "virtio-gpu-device") - } + + } else if runtime.GOARCH == "arm64" { + // No video is configured by default in ARM. This will enable it. + // TODO: This doesn't really work on arm64. It just shows a blank viewer. + cmdArgs = append(cmdArgs, "-device", "virtio-gpu-device") } if len(cfg.USBDevices) != 0 { - cmdArgs = append(cmdArgs, "-usb", "-device", "nec-usb-xhci,id=xhci") + cmdArgs = append(cmdArgs, "-device", "nec-usb-xhci,id=xhci") for _, dev := range cfg.USBDevices { - cmdArgs = append(cmdArgs, "-device", "usb-host,hostbus="+strconv.FormatUint(uint64(dev.HostBus), 10)+",hostport="+strconv.FormatUint(uint64(dev.HostPort), 10)) + cmdArgs = append(cmdArgs, "-device", "usb-host,vendorid=0x"+hex.EncodeToString(utils.Uint16ToBytesBE(dev.VendorID))+",productid=0x"+hex.EncodeToString(utils.Uint16ToBytesBE(dev.ProductID))) } }