]>
Commit | Line | Data |
---|---|---|
de3d4ac4 DC |
1 | package PVE::QemuServer::USB; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::QemuServer::PCI qw(print_pci_addr); | |
3deccbd7 | 6 | use PVE::QemuServer::Machine; |
4862922a | 7 | use PVE::QemuServer::Helpers qw(min_version windows_version); |
de3d4ac4 DC |
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 | ||
0c3d18ef DC |
17 | my $OLD_MAX_USB = 5; |
18 | ||
de3d4ac4 DC |
19 | sub parse_usb_device { |
20 | my ($value) = @_; | |
21 | ||
d1c1af4b | 22 | return if !$value; |
de3d4ac4 DC |
23 | |
24 | my $res = {}; | |
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+)*)$/) { | |
29 | $res->{hostbus} = $1; | |
30 | $res->{hostport} = $2; | |
31 | } elsif ($value =~ m/^spice$/i) { | |
32 | $res->{spice} = 1; | |
33 | } else { | |
d1c1af4b | 34 | return; |
de3d4ac4 DC |
35 | } |
36 | ||
37 | return $res; | |
38 | } | |
39 | ||
871ebe17 | 40 | my sub assert_usb_index_is_useable { |
0c3d18ef DC |
41 | my ($index, $use_qemu_xhci) = @_; |
42 | ||
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; | |
45 | ||
46 | return undef; | |
47 | } | |
48 | ||
de3d4ac4 | 49 | sub get_usb_controllers { |
4862922a | 50 | my ($conf, $bridges, $arch, $machine, $format, $max_usb_devices, $machine_version) = @_; |
de3d4ac4 DC |
51 | |
52 | my $devices = []; | |
53 | my $pciaddr = ""; | |
54 | ||
4862922a DC |
55 | my $ostype = $conf->{ostype}; |
56 | ||
57 | my $use_qemu_xhci = min_version($machine_version, 7, 1) | |
58 | && defined($ostype) && ($ostype eq 'l26' || windows_version($ostype) > 7); | |
f6b24f42 | 59 | my $is_q35 = PVE::QemuServer::Machine::machine_type_is_q35($conf); |
4862922a | 60 | |
d559309f WB |
61 | if ($arch eq 'aarch64') { |
62 | $pciaddr = print_pci_addr('ehci', $bridges, $arch, $machine); | |
63 | push @$devices, '-device', "usb-ehci,id=ehci$pciaddr"; | |
f6b24f42 | 64 | } elsif (!$is_q35) { |
d559309f | 65 | $pciaddr = print_pci_addr("piix3", $bridges, $arch, $machine); |
de3d4ac4 | 66 | push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2"; |
de3d4ac4 DC |
67 | } |
68 | ||
e68881e4 TL |
69 | my ($use_usb2, $use_usb3) = 0; |
70 | my $any_usb = 0; | |
de3d4ac4 DC |
71 | for (my $i = 0; $i < $max_usb_devices; $i++) { |
72 | next if !$conf->{"usb$i"}; | |
871ebe17 | 73 | assert_usb_index_is_useable($i, $use_qemu_xhci); |
e68881e4 TL |
74 | my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{"usb$i"}) } or next; |
75 | $any_usb = 1; | |
4862922a | 76 | $use_usb3 = 1 if $d->{usb3}; |
e68881e4 TL |
77 | $use_usb2 = 1 if !$d->{usb3}; |
78 | } | |
79 | ||
f6b24f42 | 80 | if (!$use_qemu_xhci && !$is_q35 && $use_usb2 && $arch ne 'aarch64') { |
e68881e4 TL |
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'; | |
de3d4ac4 DC |
83 | } |
84 | ||
d559309f | 85 | $pciaddr = print_pci_addr("xhci", $bridges, $arch, $machine); |
e68881e4 | 86 | if ($use_qemu_xhci && $any_usb) { |
4862922a | 87 | push @$devices, '-device', print_qemu_xhci_controller($pciaddr); |
e68881e4 TL |
88 | } elsif ($use_usb3) { |
89 | push @$devices, '-device', "nec-usb-xhci,id=xhci$pciaddr"; | |
4862922a | 90 | } |
de3d4ac4 DC |
91 | |
92 | return @$devices; | |
93 | } | |
94 | ||
95 | sub get_usb_devices { | |
4862922a | 96 | my ($conf, $format, $max_usb_devices, $features, $bootorder, $machine_version) = @_; |
de3d4ac4 DC |
97 | |
98 | my $devices = []; | |
99 | ||
4862922a DC |
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); | |
103 | ||
de3d4ac4 | 104 | for (my $i = 0; $i < $max_usb_devices; $i++) { |
2141a802 SR |
105 | my $devname = "usb$i"; |
106 | next if !$conf->{$devname}; | |
871ebe17 | 107 | assert_usb_index_is_useable($i, $use_qemu_xhci); |
2141a802 | 108 | my $d = eval { PVE::JSONSchema::parse_property_string($format,$conf->{$devname}) }; |
de3d4ac4 DC |
109 | next if !$d; |
110 | ||
342f0493 | 111 | my $port = $use_qemu_xhci ? $i + 1 : undef; |
4862922a | 112 | |
de3d4ac4 DC |
113 | if (defined($d->{host})) { |
114 | my $hostdevice = parse_usb_device($d->{host}); | |
115 | $hostdevice->{usb3} = $d->{usb3}; | |
47717a90 | 116 | if ($hostdevice->{spice}) { |
ae36393d AL |
117 | # usb redir support for spice |
118 | my $bus = 'ehci'; | |
4862922a | 119 | $bus = 'xhci' if ($hostdevice->{usb3} && $features->{spice_usb3}) || $use_qemu_xhci; |
ae36393d | 120 | |
de3d4ac4 | 121 | push @$devices, '-chardev', "spicevmc,id=usbredirchardev$i,name=usbredir"; |
4862922a | 122 | push @$devices, '-device', print_spice_usbdevice($i, $bus, $port); |
2141a802 SR |
123 | |
124 | warn "warning: spice usb port set as bootdevice, ignoring\n" if $bootorder->{$devname}; | |
de3d4ac4 | 125 | } else { |
4862922a | 126 | push @$devices, '-device', print_usbdevice_full($conf, $devname, $hostdevice, $bootorder, $port); |
de3d4ac4 DC |
127 | } |
128 | } | |
129 | } | |
130 | ||
131 | return @$devices; | |
132 | } | |
133 | ||
4862922a DC |
134 | sub print_qemu_xhci_controller { |
135 | my ($pciaddr) = @_; | |
136 | return "qemu-xhci,p2=15,p3=15,id=xhci$pciaddr"; | |
137 | } | |
138 | ||
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"; | |
144 | } | |
145 | return $device; | |
146 | } | |
147 | ||
de3d4ac4 | 148 | sub print_usbdevice_full { |
4862922a | 149 | my ($conf, $deviceid, $device, $bootorder, $port) = @_; |
de3d4ac4 DC |
150 | |
151 | return if !$device; | |
152 | my $usbdevice = "usb-host"; | |
153 | ||
4862922a | 154 | # if it is a usb3 device or with newer qemu, attach it to the xhci controller, else omit the bus option |
342f0493 | 155 | if ($device->{usb3} || defined($port)) { |
de3d4ac4 | 156 | $usbdevice .= ",bus=xhci.0"; |
4862922a | 157 | $usbdevice .= ",port=$port" if defined($port); |
de3d4ac4 DC |
158 | } |
159 | ||
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}"; | |
b06a2492 DC |
164 | } else { |
165 | die "no usb id or path given\n"; | |
de3d4ac4 DC |
166 | } |
167 | ||
168 | $usbdevice .= ",id=$deviceid"; | |
2141a802 | 169 | $usbdevice .= ",bootindex=$bootorder->{$deviceid}" if $bootorder->{$deviceid}; |
de3d4ac4 DC |
170 | return $usbdevice; |
171 | } | |
172 | ||
173 | 1; |