Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Open

Usb #284

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ Requirements

- disable_emulation: Disable sound card emulation, set this option to true if the sound card is passthrough in `[passthrough]`

### [usb]
- usb_tablet: set `true` to enable usb tablet, by default usb tablet is disabled.

### [extra]
- cmd: Set extra command.
- service: Set extra services, use `:` split different services.
Expand Down
1 change: 1 addition & 0 deletions src/guest/config_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ map<string_view, vector<string_view>> kConfigMap = {
{ kGroupAudio, { kDisableEmul } },
{ kGroupMed, { kMedBattery, kMedThermal, kMedCamera } },
{ kGroupService, { kServTimeKeep, kServPmCtrl, kServVinput } },
{ kGroupUsb, { kUsbTablet } },
{ kGroupExtra, { kExtraCmd, kExtraService, kExtraPwrCtrlMultiOS } }
};

Expand Down
3 changes: 3 additions & 0 deletions src/guest/config_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ constexpr char kGroupPciPt[] = "passthrough";
constexpr char kGroupAudio[] = "audio";
constexpr char kGroupMed[] = "mediation";
constexpr char kGroupService[] = "guest_control";
constexpr char kGroupUsb[] = "usb";
constexpr char kGroupExtra[] = "extra";

/* Keys */
Expand Down Expand Up @@ -88,6 +89,8 @@ constexpr char kServTimeKeep[] = "time_keep";
constexpr char kServPmCtrl[] = "pm_control";
constexpr char kServVinput[] = "vinput";

constexpr char kUsbTablet[] = "usb_tablet";

constexpr char kExtraCmd[] = "cmd";
constexpr char kExtraService[] = "service";
constexpr char kExtraPwrCtrlMultiOS[] = "pwr_ctrl_multios";
Expand Down
40 changes: 25 additions & 15 deletions src/guest/vm_builder_qemu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ static int SetAvailableVf(void) {
execQuantumPath.append(std::to_string(i) + kGtExecQuantumMs);
WriteSysFile(execQuantumPath.c_str(), std::to_string(kExecQuantum));
return i;
}
}
}

LOG(error) << "Failed to find 1 available VF!";
Expand Down Expand Up @@ -285,9 +285,9 @@ void VmBuilderQemu::BuildExtraGuestPmCtrlCmd(void) {
std::string ex_cmd = cfg_.GetValue(kGroupExtra, kExtraPwrCtrlMultiOS);
if (ex_cmd == "true") {
static std::string socketPath;
for(int avail = 0; avail < MAX_NUM_GUEST; avail++) {
for (int avail = 0; avail < MAX_NUM_GUEST; avail++) {
socketPath = kQmpPowerSocket + std::to_string(avail);
if(access(socketPath.c_str(), F_OK) != 0) {
if (access(socketPath.c_str(), F_OK) != 0) {
std::string qmpCmd = " -qmp unix:" + socketPath + ",server,nowait";
emul_cmd_.append(qmpCmd);
break;
Expand All @@ -297,8 +297,9 @@ void VmBuilderQemu::BuildExtraGuestPmCtrlCmd(void) {
end_call_.emplace([](){
boost::filesystem::remove(socketPath);
});
} else
} else {
return;
}
}

enum PciPassthroughAction {
Expand Down Expand Up @@ -485,7 +486,7 @@ static int GetUid(void) {

void VmBuilderQemu::BuildAudioCmd(void) {
int uid = GetUid();
if (cfg_.GetValue(kGroupAudio, kDisableEmul).compare("true") == 0)
if (cfg_.GetValue(kGroupAudio, kDisableEmul).compare("true") == 0)
return;

emul_cmd_.append(" -device intel-hda"
Expand All @@ -494,6 +495,15 @@ void VmBuilderQemu::BuildAudioCmd(void) {
"in.fixed-settings=off,out.fixed-settings=off,server=/run/user/1000/pulse/native");
}

void VmBuilderQemu::BuildUsbCmd(void) {
emul_cmd_.append(" -device qemu-xhci,id=xhci,p2=8,p3=8 -device usb-kbd");
if (cfg_.GetValue(kGroupUsb, kUsbTablet).compare("true") != 0) {
emul_cmd_.append(" -device usb-mouse");
} else {
emul_cmd_.append(" -device usb-tablet");
}
}

void VmBuilderQemu::BuildExtraCmd(void) {
std::string ex_cmd = cfg_.GetValue(kGroupExtra, kExtraCmd);
if (ex_cmd.empty())
Expand Down Expand Up @@ -543,10 +553,7 @@ void VmBuilderQemu::BuildFixedCmd(void) {
" -machine kernel_irqchip=on"
" -k en-us"
" -cpu host,-waitpkg,pmu=off"
" -enable-kvm"
" -device qemu-xhci,id=xhci,p2=8,p3=8"
" -device usb-mouse"
" -device usb-kbd");
" -enable-kvm");
emul_cmd_.append(
/* Make sure this device be the last argument */
" -device intel-iommu,device-iotlb=on,caching-mode=on"
Expand Down Expand Up @@ -630,7 +637,8 @@ void VmBuilderQemu::BuildRpmbCmd(void) {
emul_cmd_.append(" -device virtio-serial,addr=1"
" -device virtserialport,chardev=rpmb0,name=rpmb0,nr=1"
" -chardev socket,id=rpmb0,path=" + rpmb_sock);
co_procs_.emplace_back(std::make_unique<VmCoProcRpmb>(std::move(rpmb_bin), std::move(rpmb_data), std::move(rpmb_sock)));
co_procs_.emplace_back(std::make_unique<VmCoProcRpmb>(std::move(rpmb_bin), std::move(rpmb_data),
std::move(rpmb_sock)));
}
}

Expand All @@ -643,10 +651,9 @@ void VmBuilderQemu::BuildVtpmCmd(void) {
" -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-crb,tpmdev=tpm0");
co_procs_.emplace_back(std::make_unique<VmCoProcVtpm>(std::move(vtpm_bin), std::move(vtpm_data)));
}

}

void VmBuilderQemu::InitAafCfg(void){
void VmBuilderQemu::InitAafCfg(void) {
std::string aaf_path = cfg_.GetValue(kGroupAaf, kAafPath);
if (!aaf_path.empty()) {
aaf_cfg_ = std::make_unique<Aaf>(aaf_path.c_str());
Expand Down Expand Up @@ -705,7 +712,8 @@ bool VmBuilderQemu::BuildVgpuCmd(void) {
return false;
}
emul_cmd_.append(" -vga none -nographic"
" -device vfio-pci,host=00:02.0,x-igd-gms=2,id=hostdev0,bus=pcie.0,addr=0x2,x-igd-opregion=on -display none");
" -device vfio-pci,host=00:02.0,x-igd-gms=2,id=hostdev0,bus=pcie.0,addr=0x2,x-igd-opregion=on"
" -display none");
if (aaf_cfg_)
aaf_cfg_->Set(kAafKeyGpuType, "gvtd");
} else if (vgpu_type.compare(kVgpuVirtio) == 0) {
Expand Down Expand Up @@ -748,7 +756,7 @@ void VmBuilderQemu::BuildVinputCmd(void) {
LOG(warning) << "vinput-manager not found";
return;
}

if (vgpu_type.compare(kVgpuGvtD) == 0) {
vinput.append(" --gvtd");
emul_cmd_.append(
Expand Down Expand Up @@ -822,7 +830,7 @@ bool VmBuilderQemu::BuildVmArgs(void) {

if (!BuildVgpuCmd())
return false;

BuildVinputCmd();

if (!BuildAafCfg())
Expand Down Expand Up @@ -856,6 +864,8 @@ bool VmBuilderQemu::BuildVmArgs(void) {

BuildAudioCmd();

BuildUsbCmd();

BuildExtraCmd();

BuildFixedCmd();
Expand Down
1 change: 1 addition & 0 deletions src/guest/vm_builder_qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class VmBuilderQemu : public VmBuilder {
void BuildGuestPmCtrlCmd(void);
void BuildExtraGuestPmCtrlCmd(void);
void BuildAudioCmd(void);
void BuildUsbCmd(void);
void BuildExtraCmd(void);

void SoundCardHook(void);
Expand Down