1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
//go:build linux
package resources
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/sys/unix"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/resources/usbid"
)
var sysBusUSB = "/sys/bus/usb/devices"
// GetUSB returns a filled api.ResourcesUSB struct ready for use by Incus.
func GetUSB() (*api.ResourcesUSB, error) {
// Load the USB database.
usbid.Load()
usb := api.ResourcesUSB{}
usb.Devices = []api.ResourcesUSBDevice{}
if !sysfsExists(sysBusUSB) {
return &usb, nil
}
// List all USB devices
entries, err := os.ReadDir(sysBusUSB)
if err != nil {
return nil, fmt.Errorf("Failed to list %q: %w", sysBusUSB, err)
}
// Get uname for driver version
uname := unix.Utsname{}
err = unix.Uname(&uname)
if err != nil {
return nil, fmt.Errorf("Failed to get uname: %w", err)
}
for _, entry := range entries {
entryName := entry.Name()
devicePath := filepath.Join(sysBusUSB, entryName)
// Skip entries without a bus address
if !sysfsExists(filepath.Join(devicePath, "busnum")) {
continue
}
devClassFile := filepath.Join(devicePath, "bDeviceClass")
if sysfsExists(devClassFile) {
content, err := os.ReadFile(devClassFile)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", devClassFile, err)
}
devClass, err := strconv.ParseUint(strings.TrimSpace(string(content)), 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse device class %q: %w", content, err)
}
// Skip USB hubs
if devClass == 9 {
continue
}
}
device := api.ResourcesUSBDevice{}
// Get bus address
device.BusAddress, err = readUint(filepath.Join(devicePath, "busnum"))
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", filepath.Join(devicePath, "busnum"), err)
}
// Get device address
device.DeviceAddress, err = readUint(filepath.Join(devicePath, "devnum"))
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", filepath.Join(devicePath, "devnum"), err)
}
// Get serial number
deviceSerialPath := filepath.Join(devicePath, "iSerial")
if sysfsExists(deviceSerialPath) {
content, err := os.ReadFile(deviceSerialPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", deviceSerialPath, err)
}
device.Serial = strings.TrimSpace(string(content))
}
// Get product ID
var productID uint64
deviceProductIDPath := filepath.Join(devicePath, "idProduct")
if sysfsExists(deviceProductIDPath) {
content, err := os.ReadFile(deviceProductIDPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", deviceProductIDPath, err)
}
device.ProductID = strings.TrimPrefix(strings.TrimSpace(string(content)), "0x")
productID, err = strconv.ParseUint(device.ProductID, 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse product ID %q: %w", device.ProductID, err)
}
}
// Get vendor ID
var vendorID uint64
deviceVendorIDPath := filepath.Join(devicePath, "idVendor")
if sysfsExists(deviceVendorIDPath) {
content, err := os.ReadFile(deviceVendorIDPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", deviceVendorIDPath, err)
}
device.VendorID = strings.TrimPrefix(strings.TrimSpace(string(content)), "0x")
vendorID, err = strconv.ParseUint(device.VendorID, 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse vendor ID %q: %w", device.VendorID, err)
}
}
// Get vendor and product name
deviceProductPath := filepath.Join(devicePath, "product")
if sysfsExists(deviceProductPath) {
content, err := os.ReadFile(deviceProductPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", deviceProductPath, err)
}
device.Product = strings.TrimSpace(string(content))
}
vendor := usbid.Vendors[usbid.ID(vendorID)]
if vendor != nil {
device.Vendor = vendor.Name
// If there's no product file, get it from usbid.
if device.Product == "" {
product := vendor.Product[usbid.ID(productID)]
if product != nil {
device.Product = product.Name
}
}
}
// Get speed
deviceSpeedPath := filepath.Join(devicePath, "speed")
if sysfsExists(deviceSpeedPath) {
content, err := os.ReadFile(deviceSpeedPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", deviceSpeedPath, err)
}
device.Speed, err = strconv.ParseFloat(strings.TrimSpace(string(content)), 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse speed %q: %w", content, err)
}
}
// List USB interfaces
subEntries, err := os.ReadDir(devicePath)
if err != nil {
return nil, fmt.Errorf("Failed to list %q: %w", devicePath, err)
}
for _, subEntry := range subEntries {
subEntryName := subEntry.Name()
subDevicePath := filepath.Join(devicePath, subEntryName)
// Skip irrelevant directories and file entries
if !subEntry.IsDir() || !strings.HasPrefix(subEntryName, entryName) {
continue
}
iface := api.ResourcesUSBDeviceInterface{}
// Get class ID
var class *usbid.Class
interfaceClassPath := filepath.Join(subDevicePath, "bInterfaceClass")
if sysfsExists(interfaceClassPath) {
content, err := os.ReadFile(interfaceClassPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", interfaceClassPath, err)
}
iface.ClassID, err = strconv.ParseUint(strings.TrimSpace(string(content)), 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse class ID %q: %w", content, err)
}
var ok bool
class, ok = usbid.Classes[usbid.ClassCode(iface.ClassID)]
if ok {
iface.Class = class.Name
}
}
// Get subclass ID
interfaceSubClassPath := filepath.Join(subDevicePath, "bInterfaceSubClass")
if sysfsExists(interfaceSubClassPath) {
content, err := os.ReadFile(interfaceSubClassPath)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", interfaceSubClassPath, err)
}
iface.SubClassID, err = strconv.ParseUint(strings.TrimSpace(string(content)), 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse subclass ID %q: %w", content, err)
}
if iface.SubClassID > 0 && class != nil {
subclass, ok := class.SubClass[usbid.ClassCode(iface.SubClassID)]
if ok {
iface.SubClass = subclass.Name
}
}
}
// Get number
interfaceNumber := filepath.Join(subDevicePath, "bInterfaceNumber")
if sysfsExists(interfaceNumber) {
content, err := os.ReadFile(interfaceNumber)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", interfaceNumber, err)
}
iface.Number, err = strconv.ParseUint(strings.TrimSpace(string(content)), 16, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse interface number %q: %w", content, err)
}
}
// Get driver
driverPath := filepath.Join(subDevicePath, "driver")
if sysfsExists(driverPath) {
linkTarget, err := filepath.EvalSymlinks(driverPath)
if err != nil {
return nil, fmt.Errorf("Failed to get driver of %q: %w", subDevicePath, err)
}
iface.Driver = filepath.Base(linkTarget)
// Try to get the version, fallback to kernel version
out, err := os.ReadFile(filepath.Join(driverPath, "module", "version"))
if err == nil {
iface.DriverVersion = strings.TrimSpace(string(out))
} else {
iface.DriverVersion = strings.TrimRight(string(uname.Release[:]), "\x00")
}
}
device.Interfaces = append(device.Interfaces, iface)
}
usb.Devices = append(usb.Devices, device)
usb.Total++
}
return &usb, nil
}
|