[pve-devel] [PATCH qemu-server 1/3] copy scan_usb from PVE::Storage

Dominik Csapak d.csapak at proxmox.com
Thu Nov 15 15:30:44 CET 2018


it makes more sense to have it here rather than in storage

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/QemuServer/USB.pm | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/PVE/QemuServer/USB.pm b/PVE/QemuServer/USB.pm
index 3d65d38..f51329b 100644
--- a/PVE/QemuServer/USB.pm
+++ b/PVE/QemuServer/USB.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use PVE::QemuServer::PCI qw(print_pci_addr);
 use PVE::JSONSchema;
+use PVE::Tools qw(file_read_firstline);
 use base 'Exporter';
 
 our @EXPORT_OK = qw(
@@ -124,4 +125,61 @@ sub print_usbdevice_full {
     return $usbdevice;
 }
 
+# idea is from usbutils package (/usr/bin/usb-devices) script
+sub __scan_usb_device {
+    my ($res, $devpath, $parent, $level) = @_;
+
+    return if ! -d $devpath;
+    return if $level && $devpath !~ m/^.*[-.](\d+)$/;
+    my $port = $level ? int($1 - 1) : 0;
+
+    my $busnum = int(file_read_firstline("$devpath/busnum"));
+    my $devnum = int(file_read_firstline("$devpath/devnum"));
+
+    my $d = {
+	port => $port,
+	level => $level,
+	busnum => $busnum,
+	devnum => $devnum,
+	speed => file_read_firstline("$devpath/speed"),
+	class => hex(file_read_firstline("$devpath/bDeviceClass")),
+	vendid => file_read_firstline("$devpath/idVendor"),
+	prodid => file_read_firstline("$devpath/idProduct"),
+    };
+
+    if ($level) {
+	my $usbpath = $devpath;
+	$usbpath =~ s|^.*/\d+\-||;
+	$d->{usbpath} = $usbpath;
+    }
+
+    my $product = file_read_firstline("$devpath/product");
+    $d->{product} = $product if $product;
+
+    my $manu = file_read_firstline("$devpath/manufacturer");
+    $d->{manufacturer} = $manu if $manu;
+
+    my $serial => file_read_firstline("$devpath/serial");
+    $d->{serial} = $serial if $serial;
+
+    push @$res, $d;
+
+    foreach my $subdev (<$devpath/$busnum-*>) {
+	next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
+	__scan_usb_device($res, $subdev, $devnum, $level + 1);
+    }
+
+};
+
+sub scan_usb {
+
+    my $devlist = [];
+
+    foreach my $device (</sys/bus/usb/devices/usb*>) {
+	__scan_usb_device($devlist, $device, 0, 0);
+    }
+
+    return $devlist;
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list