1 package PVE
::QemuServer
::USB
;
5 use PVE
::QemuServer
::PCI
qw(print_pci_addr);
6 use PVE
::QemuServer
::Machine
;
7 use PVE
::QemuServer
::Helpers
qw(min_version windows_version);
19 our $MAX_USB_DEVICES = 14;
22 my $USB_ID_RE = qr/(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})/;
23 my $USB_PATH_RE = qr/(\d+)\-(\d+(\.\d+)*)/;
30 pattern
=> qr/(?:(?:$USB_ID_RE)|(?:$USB_PATH_RE)|[Ss][Pp][Ii][Cc][Ee])/,
31 format_description
=> 'HOSTUSBDEVICE|spice',
32 description
=> <<EODESCR,
33 The Host USB device or port or the value 'spice'. HOSTUSBDEVICE syntax is:
35 'bus-port(.port)*' (decimal numbers) or
36 'vendor_id:product_id' (hexadeciaml numbers) or
39 You can use the 'lsusb -t' command to list existing usb devices.
41 NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such
42 machines - use with special care.
44 The value 'spice' can be used to add a usb redirection devices for spice.
46 Either this or the 'mapping' key must be set.
52 format_description
=> 'mapping-id',
53 format
=> 'pve-configid',
54 description
=> "The ID of a cluster wide mapping. Either this or the default-key 'host'"
60 description
=> "Specifies whether if given host option is a USB3 device or port."
61 ." For modern guests (machine version >= 7.1 and ostype l26 and windows > 7), this flag"
62 ." is irrelevant (all devices are plugged into a xhci controller).",
67 PVE
::JSONSchema
::register_format
('pve-qm-usb', $usb_fmt);
71 type
=> 'string', format
=> $usb_fmt,
72 description
=> "Configure an USB device (n is 0 to 4, for machine version >= 7.1 and ostype"
73 ." l26 or windows > 7, n can be up to 14).",
75 PVE
::JSONSchema
::register_standard_option
("pve-qm-usb", $usbdesc);
77 sub parse_usb_device
{
78 my ($value, $mapping) = @_;
80 return if $value && $mapping; # not a valid configuration
83 if (defined($value)) {
84 if ($value =~ m/^$USB_ID_RE$/) {
85 $res->{vendorid
} = $2;
86 $res->{productid
} = $4;
87 } elsif ($value =~ m/^$USB_PATH_RE$/) {
89 $res->{hostport
} = $2;
90 } elsif ($value =~ m/^spice$/i) {
93 } elsif (defined($mapping)) {
94 my $devices = PVE
::Mapping
::USB
::find_on_current_node
($mapping);
95 die "USB device mapping not found for '$mapping'\n" if !$devices || !scalar($devices->@*);
96 die "More than one USB mapping per host not supported\n" if scalar($devices->@*) > 1;
98 PVE
::Mapping
::USB
::assert_valid
($mapping, $devices->[0]);
101 die "USB Mapping invalid (hardware probably changed): $err\n";
103 my $device = $devices->[0];
105 if ($device->{path
}) {
106 $res = parse_usb_device
($device->{path
});
108 $res = parse_usb_device
($device->{id
});
115 my sub assert_usb_index_is_useable
{
116 my ($index, $use_qemu_xhci) = @_;
118 die "using usb$index is only possible with machine type >= 7.1 and ostype l26 or windows > 7\n"
119 if $index >= $OLD_MAX_USB && !$use_qemu_xhci;
124 sub get_usb_controllers
{
125 my ($conf, $bridges, $arch, $machine, $machine_version) = @_;
130 my $ostype = $conf->{ostype
};
132 my $use_qemu_xhci = min_version
($machine_version, 7, 1)
133 && defined($ostype) && ($ostype eq 'l26' || windows_version
($ostype) > 7);
134 my $is_q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
136 if ($arch eq 'aarch64') {
137 $pciaddr = print_pci_addr
('ehci', $bridges, $arch, $machine);
138 push @$devices, '-device', "usb-ehci,id=ehci$pciaddr";
140 $pciaddr = print_pci_addr
("piix3", $bridges, $arch, $machine);
141 push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
144 my ($use_usb2, $use_usb3) = 0;
146 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
147 next if !$conf->{"usb$i"};
148 assert_usb_index_is_useable
($i, $use_qemu_xhci);
149 my $d = eval { PVE
::JSONSchema
::parse_property_string
($usb_fmt, $conf->{"usb$i"}) } or next;
151 $use_usb3 = 1 if $d->{usb3
};
152 $use_usb2 = 1 if !$d->{usb3
};
155 if (!$use_qemu_xhci && !$is_q35 && $use_usb2 && $arch ne 'aarch64') {
156 # include usb device config if still on x86 before-xhci machines and if USB 3 is not used
157 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg';
160 $pciaddr = print_pci_addr
("xhci", $bridges, $arch, $machine);
161 if ($use_qemu_xhci && $any_usb) {
162 push @$devices, '-device', print_qemu_xhci_controller
($pciaddr);
163 } elsif ($use_usb3) {
164 push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr";
170 sub get_usb_devices
{
171 my ($conf, $features, $bootorder, $machine_version) = @_;
175 my $ostype = $conf->{ostype
};
176 my $use_qemu_xhci = min_version
($machine_version, 7, 1)
177 && defined($ostype) && ($ostype eq 'l26' || windows_version
($ostype) > 7);
179 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
180 my $devname = "usb$i";
181 next if !$conf->{$devname};
182 assert_usb_index_is_useable
($i, $use_qemu_xhci);
183 my $d = eval { PVE
::JSONSchema
::parse_property_string
($usb_fmt, $conf->{$devname}) };
186 my $port = $use_qemu_xhci ?
$i + 1 : undef;
188 if ($d->{host
} && $d->{host
} =~ m/^spice$/) {
189 # usb redir support for spice
191 $bus = 'xhci' if ($d->{usb3
} && $features->{spice_usb3
}) || $use_qemu_xhci;
193 push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
194 push @$devices, '-device', print_spice_usbdevice
($i, $bus, $port);
196 warn "warning: spice usb port set as bootdevice, ignoring\n" if $bootorder->{$devname};
198 push @$devices, '-device', print_usbdevice_full
($conf, $devname, $d, $bootorder, $port);
205 sub print_qemu_xhci_controller
{
207 return "qemu-xhci,p2=15,p3=15,id=xhci$pciaddr";
210 sub print_spice_usbdevice
{
211 my ($index, $bus, $port) = @_;
212 my $device = "usb-redir,chardev=usbredirchardev$index,id=usbredirdev$index,bus=$bus.0";
213 if (defined($port)) {
214 $device .= ",port=$port";
219 sub print_usbdevice_full
{
220 my ($conf, $deviceid, $device, $bootorder, $port) = @_;
223 my $usbdevice = "usb-host";
225 # if it is a usb3 device or with newer qemu, attach it to the xhci controller, else omit the bus option
226 if ($device->{usb3
} || defined($port)) {
227 $usbdevice .= ",bus=xhci.0";
228 $usbdevice .= ",port=$port" if defined($port);
231 my $parsed = parse_usb_device
($device->{host
}, $device->{mapping
});
233 if (defined($parsed->{vendorid
}) && defined($parsed->{productid
})) {
234 $usbdevice .= ",vendorid=0x$parsed->{vendorid},productid=0x$parsed->{productid}";
235 } elsif (defined($parsed->{hostbus
}) && defined($parsed->{hostport
})) {
236 $usbdevice .= ",hostbus=$parsed->{hostbus},hostport=$parsed->{hostport}";
238 die "no usb id or path given\n";
241 $usbdevice .= ",id=$deviceid";
242 $usbdevice .= ",bootindex=$bootorder->{$deviceid}" if $bootorder->{$deviceid};