]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/USB.pm
fix #4324: USB: use qemu-xhci for machine versions >= 7.1
[qemu-server.git] / PVE / QemuServer / USB.pm
1 package PVE::QemuServer::USB;
2
3 use strict;
4 use warnings;
5 use PVE::QemuServer::PCI qw(print_pci_addr);
6 use PVE::QemuServer::Machine;
7 use PVE::QemuServer::Helpers qw(min_version windows_version);
8 use PVE::JSONSchema;
9 use base 'Exporter';
10
11 our @EXPORT_OK = qw(
12 parse_usb_device
13 get_usb_controllers
14 get_usb_devices
15 );
16
17 sub parse_usb_device {
18 my ($value) = @_;
19
20 return if !$value;
21
22 my $res = {};
23 if ($value =~ m/^(0x)?([0-9A-Fa-f]{4}):(0x)?([0-9A-Fa-f]{4})$/) {
24 $res->{vendorid} = $2;
25 $res->{productid} = $4;
26 } elsif ($value =~ m/^(\d+)\-(\d+(\.\d+)*)$/) {
27 $res->{hostbus} = $1;
28 $res->{hostport} = $2;
29 } elsif ($value =~ m/^spice$/i) {
30 $res->{spice} = 1;
31 } else {
32 return;
33 }
34
35 return $res;
36 }
37
38 sub get_usb_controllers {
39 my ($conf, $bridges, $arch, $machine, $format, $max_usb_devices, $machine_version) = @_;
40
41 my $devices = [];
42 my $pciaddr = "";
43
44 my $ostype = $conf->{ostype};
45
46 my $use_qemu_xhci = min_version($machine_version, 7, 1)
47 && defined($ostype) && ($ostype eq 'l26' || windows_version($ostype) > 7);
48
49 if ($arch eq 'aarch64') {
50 $pciaddr = print_pci_addr('ehci', $bridges, $arch, $machine);
51 push @$devices, '-device', "usb-ehci,id=ehci$pciaddr";
52 } elsif (!PVE::QemuServer::Machine::machine_type_is_q35($conf)) {
53 $pciaddr = print_pci_addr("piix3", $bridges, $arch, $machine);
54 push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
55
56 if (!$use_qemu_xhci) {
57 my $use_usb2 = 0;
58 for (my $i = 0; $i < $max_usb_devices; $i++) {
59 next if !$conf->{"usb$i"};
60 my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) };
61 next if !$d || $d->{usb3}; # do not add usb2 controller if we have only usb3 devices
62 $use_usb2 = 1;
63 }
64 # include usb device config
65 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
66 }
67 }
68
69 # add usb3 controller if needed
70
71 my $use_usb3 = 0;
72 my $use_usb = 0;
73 for (my $i = 0; $i < $max_usb_devices; $i++) {
74 next if !$conf->{"usb$i"};
75 my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) };
76 next if !$d;
77 $use_usb = 1;
78 $use_usb3 = 1 if $d->{usb3};
79 }
80
81 $pciaddr = print_pci_addr("xhci", $bridges, $arch, $machine);
82 if ($use_qemu_xhci && $use_usb) {
83 push @$devices, '-device', print_qemu_xhci_controller($pciaddr);
84 } else {
85 push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr" if $use_usb3;
86 }
87
88 return @$devices;
89 }
90
91 sub get_usb_devices {
92 my ($conf, $format, $max_usb_devices, $features, $bootorder, $machine_version) = @_;
93
94 my $devices = [];
95
96 my $ostype = $conf->{ostype};
97 my $use_qemu_xhci = min_version($machine_version, 7, 1)
98 && defined($ostype) && ($ostype eq 'l26' || windows_version($ostype) > 7);
99
100 for (my $i = 0; $i < $max_usb_devices; $i++) {
101 my $devname = "usb$i";
102 next if !$conf->{$devname};
103 my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{$devname}) };
104 next if !$d;
105
106 my $port;
107 if ($use_qemu_xhci) {
108 $port = $i + 1;
109 }
110
111 if (defined($d->{host})) {
112 my $hostdevice = parse_usb_device($d->{host});
113 $hostdevice->{usb3} = $d->{usb3};
114 if ($hostdevice->{spice}) {
115 # usb redir support for spice
116 my $bus = 'ehci';
117 $bus = 'xhci' if ($hostdevice->{usb3} && $features->{spice_usb3}) || $use_qemu_xhci;
118
119 push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir";
120 push @$devices, '-device', print_spice_usbdevice($i, $bus, $port);
121
122 warn "warning: spice usb port set as bootdevice, ignoring\n" if $bootorder->{$devname};
123 } else {
124 push @$devices, '-device', print_usbdevice_full($conf, $devname, $hostdevice, $bootorder, $port);
125 }
126 }
127 }
128
129 return @$devices;
130 }
131
132 sub print_qemu_xhci_controller {
133 my ($pciaddr) = @_;
134 return "qemu-xhci,p2=15,p3=15,id=xhci$pciaddr";
135 }
136
137 sub print_spice_usbdevice {
138 my ($index, $bus, $port) = @_;
139 my $device = "usb-redir,chardev=usbredirchardev$index,id=usbredirdev$index,bus=$bus.0";
140 if (defined($port)) {
141 $device .= ",port=$port";
142 }
143 return $device;
144 }
145
146 sub print_usbdevice_full {
147 my ($conf, $deviceid, $device, $bootorder, $port) = @_;
148
149 return if !$device;
150 my $usbdevice = "usb-host";
151
152 # if it is a usb3 device or with newer qemu, attach it to the xhci controller, else omit the bus option
153 if($device->{usb3} || defined($port)) {
154 $usbdevice .= ",bus=xhci.0";
155 $usbdevice .= ",port=$port" if defined($port);
156 }
157
158 if (defined($device->{vendorid}) && defined($device->{productid})) {
159 $usbdevice .= ",vendorid=0x$device->{vendorid},productid=0x$device->{productid}";
160 } elsif (defined($device->{hostbus}) && defined($device->{hostport})) {
161 $usbdevice .= ",hostbus=$device->{hostbus},hostport=$device->{hostport}";
162 } else {
163 die "no usb id or path given\n";
164 }
165
166 $usbdevice .= ",id=$deviceid";
167 $usbdevice .= ",bootindex=$bootorder->{$deviceid}" if $bootorder->{$deviceid};
168 return $usbdevice;
169 }
170
171 1;