]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/USB.pm
10c9bca17d06ddb2eb5ce861d2f08b4828619d6a
1 package PVE
::QemuServer
::USB
;
5 use PVE
::QemuServer
::PCI
qw(print_pci_addr);
6 use PVE
::QemuServer
::Machine
;
16 sub parse_usb_device
{
22 if ($value =~ m/^(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/) {
23 $res->{vendorid
} = $2;
24 $res->{productid
} = $4;
25 } elsif ($value =~ m/^(\d+)\-(\d+(\.\d+)*)$/) {
27 $res->{hostport
} = $2;
28 } elsif ($value =~ m/^spice$/i) {
37 sub get_usb_controllers
{
38 my ($conf, $bridges, $arch, $machine, $format, $max_usb_devices) = @_;
43 if ($arch eq 'aarch64') {
44 $pciaddr = print_pci_addr
('ehci', $bridges, $arch, $machine);
45 push @$devices, '-device', "usb-ehci,id=ehci$pciaddr";
46 } elsif (!PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf)) {
47 $pciaddr = print_pci_addr
("piix3", $bridges, $arch, $machine);
48 push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
51 for (my $i = 0; $i < $max_usb_devices; $i++) {
52 next if !$conf->{"usb$i"};
53 my $d = eval { PVE
::JSONSchema
::parse_property_string
($format,$conf->{"usb$i"}) };
54 next if !$d || $d->{usb3
}; # do not add usb2 controller if we have only usb3 devices
57 # include usb device config
58 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
61 # add usb3 controller if needed
64 for (my $i = 0; $i < $max_usb_devices; $i++) {
65 next if !$conf->{"usb$i"};
66 my $d = eval { PVE
::JSONSchema
::parse_property_string
($format,$conf->{"usb$i"}) };
67 next if !$d || !$d->{usb3
};
71 $pciaddr = print_pci_addr
("xhci", $bridges, $arch, $machine);
72 push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr" if $use_usb3;
78 my ($conf, $format, $max_usb_devices, $features, $bootorder) = @_;
82 for (my $i = 0; $i < $max_usb_devices; $i++) {
83 my $devname = "usb$i";
84 next if !$conf->{$devname};
85 my $d = eval { PVE
::JSONSchema
::parse_property_string
($format,$conf->{$devname}) };
88 if (defined($d->{host
})) {
89 my $hostdevice = parse_usb_device
($d->{host
});
90 $hostdevice->{usb3
} = $d->{usb3
};
91 if ($hostdevice->{spice
}) {
92 # usb redir support for spice
94 $bus = 'xhci' if $hostdevice->{usb3
} && $features->{spice_usb3
};
96 push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
97 push @$devices, '-device', "usb-redir,chardev=usbredirchardev$i,id=usbredirdev$i,bus=$bus.0";
99 warn "warning: spice usb port set as bootdevice, ignoring\n" if $bootorder->{$devname};
101 push @$devices, '-device', print_usbdevice_full
($conf, $devname, $hostdevice, $bootorder);
109 sub print_usbdevice_full
{
110 my ($conf, $deviceid, $device, $bootorder) = @_;
113 my $usbdevice = "usb-host";
115 # if it is a usb3 device, attach it to the xhci controller, else omit the bus option
116 if($device->{usb3
}) {
117 $usbdevice .= ",bus=xhci.0";
120 if (defined($device->{vendorid
}) && defined($device->{productid
})) {
121 $usbdevice .= ",vendorid=0x$device->{vendorid},productid=0x$device->{productid}";
122 } elsif (defined($device->{hostbus
}) && defined($device->{hostport
})) {
123 $usbdevice .= ",hostbus=$device->{hostbus},hostport=$device->{hostport}";
125 die "no usb id or path given\n";
128 $usbdevice .= ",id=$deviceid";
129 $usbdevice .= ",bootindex=$bootorder->{$deviceid}" if $bootorder->{$deviceid};