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 sub parse_usb_device
{
25 if ($value =~ m/^(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/) {
26 $res->{vendorid
} = $2;
27 $res->{productid
} = $4;
28 } elsif ($value =~ m/^(\d+)\-(\d+(\.\d+)*)$/) {
30 $res->{hostport
} = $2;
31 } elsif ($value =~ m/^spice$/i) {
40 my sub assert_usb_index_is_useable
{
41 my ($index, $use_qemu_xhci) = @_;
43 die "using usb$index is only possible with machine type >= 7.1 and ostype l26 or windows > 7\n"
44 if $index >= $OLD_MAX_USB && !$use_qemu_xhci;
49 sub get_usb_controllers
{
50 my ($conf, $bridges, $arch, $machine, $format, $max_usb_devices, $machine_version) = @_;
55 my $ostype = $conf->{ostype
};
57 my $use_qemu_xhci = min_version
($machine_version, 7, 1)
58 && defined($ostype) && ($ostype eq 'l26' || windows_version
($ostype) > 7);
59 my $is_q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
61 if ($arch eq 'aarch64') {
62 $pciaddr = print_pci_addr
('ehci', $bridges, $arch, $machine);
63 push @$devices, '-device', "usb-ehci,id=ehci$pciaddr";
65 $pciaddr = print_pci_addr
("piix3", $bridges, $arch, $machine);
66 push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
69 my ($use_usb2, $use_usb3) = 0;
71 for (my $i = 0; $i < $max_usb_devices; $i++) {
72 next if !$conf->{"usb$i"};
73 assert_usb_index_is_useable
($i, $use_qemu_xhci);
74 my $d = eval { PVE
::JSONSchema
::parse_property_string
($format,$conf->{"usb$i"}) } or next;
76 $use_usb3 = 1 if $d->{usb3
};
77 $use_usb2 = 1 if !$d->{usb3
};
80 if (!$use_qemu_xhci && !$is_q35 && $use_usb2 && $arch ne 'aarch64') {
81 # include usb device config if still on x86 before-xhci machines and if USB 3 is not used
82 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg';
85 $pciaddr = print_pci_addr
("xhci", $bridges, $arch, $machine);
86 if ($use_qemu_xhci && $any_usb) {
87 push @$devices, '-device', print_qemu_xhci_controller
($pciaddr);
89 push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr";
96 my ($conf, $format, $max_usb_devices, $features, $bootorder, $machine_version) = @_;
100 my $ostype = $conf->{ostype
};
101 my $use_qemu_xhci = min_version
($machine_version, 7, 1)
102 && defined($ostype) && ($ostype eq 'l26' || windows_version
($ostype) > 7);
104 for (my $i = 0; $i < $max_usb_devices; $i++) {
105 my $devname = "usb$i";
106 next if !$conf->{$devname};
107 assert_usb_index_is_useable
($i, $use_qemu_xhci);
108 my $d = eval { PVE
::JSONSchema
::parse_property_string
($format,$conf->{$devname}) };
111 my $port = $use_qemu_xhci ?
$i + 1 : undef;
113 if (defined($d->{host
})) {
114 my $hostdevice = parse_usb_device
($d->{host
});
115 $hostdevice->{usb3
} = $d->{usb3
};
116 if ($hostdevice->{spice
}) {
117 # usb redir support for spice
119 $bus = 'xhci' if ($hostdevice->{usb3
} && $features->{spice_usb3
}) || $use_qemu_xhci;
121 push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
122 push @$devices, '-device', print_spice_usbdevice
($i, $bus, $port);
124 warn "warning: spice usb port set as bootdevice, ignoring\n" if $bootorder->{$devname};
126 push @$devices, '-device', print_usbdevice_full
($conf, $devname, $hostdevice, $bootorder, $port);
134 sub print_qemu_xhci_controller
{
136 return "qemu-xhci,p2=15,p3=15,id=xhci$pciaddr";
139 sub print_spice_usbdevice
{
140 my ($index, $bus, $port) = @_;
141 my $device = "usb-redir,chardev=usbredirchardev$index,id=usbredirdev$index,bus=$bus.0";
142 if (defined($port)) {
143 $device .= ",port=$port";
148 sub print_usbdevice_full
{
149 my ($conf, $deviceid, $device, $bootorder, $port) = @_;
152 my $usbdevice = "usb-host";
154 # if it is a usb3 device or with newer qemu, attach it to the xhci controller, else omit the bus option
155 if ($device->{usb3
} || defined($port)) {
156 $usbdevice .= ",bus=xhci.0";
157 $usbdevice .= ",port=$port" if defined($port);
160 if (defined($device->{vendorid
}) && defined($device->{productid
})) {
161 $usbdevice .= ",vendorid=0x$device->{vendorid},productid=0x$device->{productid}";
162 } elsif (defined($device->{hostbus
}) && defined($device->{hostport
})) {
163 $usbdevice .= ",hostbus=$device->{hostbus},hostport=$device->{hostport}";
165 die "no usb id or path given\n";
168 $usbdevice .= ",id=$deviceid";
169 $usbdevice .= ",bootindex=$bootorder->{$deviceid}" if $bootorder->{$deviceid};